Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I came up with a way to organize animation completion code that works really well. First, I defined a custom type for a block of code to run when an animation completes:</p> <pre><code>typedef void (^animationCompletionBlock)(void); </code></pre> <p>I define a constant that I use to add a custom key-value pair to my animation:</p> <pre><code>#define kAnimationCompletionBlock @"animationCompletionBlock" </code></pre> <p>Then, when I create an animation, if I want it to execute a block of code when it finishes, I add a custom property to my animation that contains the block of code I want to execute:</p> <pre><code>animationCompletionBlock theBlock; theBlock = ^void(void) { someButton.enabled = TRUE; NSLog(@"Animation complete"); //whatever other code you want to do on completion } [myAnimation setValue: theBlock forKey: kAnimationCompletionBlock]; </code></pre> <p>I set the view controller to be the animation's delegate:</p> <pre><code>myAnimation.delegate = self </code></pre> <p>Finally, I write a general-purpose animation completion method that looks for an animation completion block attached to the animation, and executes it if it finds it:</p> <pre><code>/* This method looks for a value added to the animation that just completed with the key kAnimationCompletionBlock. If it exists, it assumes it is a code block of type animationCompletionBlock, and executes the code block. This allows you to add a custom block of completion code to any animation or animation group, rather than Having a big complicated switch statement in your animationDidStop:finished: method with global animation ompletion code. (Note that the system won't call the animationDidStop:finished method for individual animations in an animation group - it will only call the completion method for the entire group. Thus, if you want to run code after part of an animation group completes, you have to set up a manual timer.) */ - (void)animationDidStop:(CAAnimation *)theAnimation finished:(BOOL)flag { animationCompletionBlock theBlock = [theAnimation valueForKey: kAnimationCompletionBlock]; if (theBlock) theBlock(); } </code></pre> <p>In addition to being very clean, this approach also lets your animation completion code have access to local variables that are inside the scope where you define the block. That solves the problem of passing information to your completion method, which can be difficult.</p> <p>You can see this technique in a working example program I posted to Github:</p> <p><strong><a href="https://github.com/DuncanMC/iOS-CAAnimation-group-demo" rel="nofollow">Core Animation demo on Github, including completion block code</a></strong></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload