Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7837281
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T14:33:26+00:00 2026-06-02T14:33:26+00:00

I’m working on some CoreAnimation stuff. A navigation controller with a few view controllers.

  • 0

I’m working on some CoreAnimation stuff. A navigation controller with a few view controllers. And the view controllers have UISCrollViews for different “pages” of a “brochure.” On each page, there might be some animation that gets triggered when the user flips to that page.

I was trying something like this (for one-shot animations).

void (^animationBlock)() =
  ^{
    static bool alreadyTriggered = NO;
    if(alreadyTriggered)
      return;

    [CATransaction begin];
    [CATransaction setCompletionBlock:
     ^{
       alreadyTriggered = YES;
     }];

    // Do me some animations...

    [CATransaction commit];
  };

  NSMutableDictionary* pageBlocks = [[NSMutableDictionary alloc] init];
  [pageBlocks setObject:[animationBlock copy] forKey:<animation's name>];
  [self.animationBlocks setObject:pageBlocks forKey:<some page number>];

  [pageBlocks release];
  [animationBlock release];

“animation’s name” and “some page number” are placeholders for the sake of explanation (they are arbitrary NSString literals).

And the code that triggers these animations is:

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
  int pageNumber = floor(self.scrollView.contentOffset.x / self.scrollView.frame.size.width);
  NSMutableDictionary* pageBLocks = [self.animationBlocks objectForKey:[NSString stringWithFormat:@"page%i",pageNumber]];
  [CATransaction begin];
  for(id key in pageBLocks)
    ((void (^)())[pageBLocks objectForKey:key])();
  [CATransaction commit];
 }

So far so good, only that If I pop the brochure from the navigation controller (aka calls dealloc on the brochure) and then push it in again, the static bool is still set.

My thoughts:

– am I retaining the block?
I don’t know.. I’m calling release after adding it (with copy) to the dictionary and also the brochure’s dealloc method calls release on the dictionary.

– am I keeping another copy of the static bool somewhere?
My first bool is allocated when I declare the block as static within the scope of a method.. well depends on Objective-C’s activation record scheme which I haven’t looked into. But assuming so, that copy is gone when releasing the object on popViewcOntroller. And another copy of it from invoking copy on the block when adding it to the dictionary should be released when the dictionary is killed?

am I retaining the whole brochure object itself? I didn’t completely get it from the Apple docs, but they say if I access an instance variable by reference I retain self. I tried releasing self from inside the block and everything keeps running just fine…?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T14:33:32+00:00Added an answer on June 2, 2026 at 2:33 pm

    Make the block return a value and then decide whether or not to remove the block from the dictionary.

    // ...
        BOOL shouldRemove = block();
    
        if (shouldRemove) {
            [pageBlocks removeObjectForKey:key];
        }
    // ...
    

    Let’s test the static variable

    @interface TestClass : NSObject
    @end
    
    @implementation TestClass
    
    - (void(^)(void))block;
    {
        return [[^{
    
            static BOOL staticBOOL = NO;
    
            NSLog(@"%d", staticBOOL);
    
            staticBOOL = YES;
    
        } copy] autorelease];
    }  
    
    @end
    
    int main(int argc, char *argv[]) {
        NSAutoreleasePool *p = [[NSAutoreleasePool alloc] init];
    
        TestClass *test1 = [[TestClass alloc] init];
        test1.block();
        test1.block();
    
        TestClass *test2 = [[TestClass alloc] init];
        test2.block();
        test2.block();
    
        [p release];
    }
    

    This outputs

    #=> 2012-04-23 00:43:38.501 Untitled[8380:707] 0
    #=> 2012-04-23 00:43:38.503 Untitled[8380:707] 1
    #=> 2012-04-23 00:43:38.503 Untitled[8380:707] 1
    #=> 2012-04-23 00:43:38.504 Untitled[8380:707] 1
    

    How do we solve the problem? I would probably remove the object from the dictionary once it had been executed like this

    - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView;
    {
        NSInteger pageNumber = floor(self.scrollView.contentOffset.x / self.scrollView.frame.size.width);
        NSMutableDictionary *pageBlocks = [self.animationBlocks objectForKey:[NSString stringWithFormat:@"page%i", pageNumber]];
    
        [CATransaction begin];
    
        for (id key in [pageBlocks copy]) {
            void (^block)(void) = [pageBlocks objectForKey:key];
            if (block) {
                block();
            }
            [pageBlocks removeObjectForKey:key];
        }
    
        [CATransaction commit];
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have just tried to save a simple *.rtf file with some websites and
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have an MVC Razor view @{ ViewBag.Title = Index; var c = (char)146;
I have some data like this: 1 2 3 4 5 9 2 6
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a jquery bug and I've been looking for hours now, I can't
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have a French site that I want to parse, but am running into

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.