Wednesday, June 12, 2013

CATransaction setCompletionBlock example in Objective C (iOS).

CATransaction setCompletionBlock

Sets the completion block object.

+ (void)setCompletionBlock:(void (^)(void))block

Parameters of [CATransaction setCompletionBlock]
block
A block object called when animations for this transaction group are completed.
The block object takes no parameters and returns no value.

Discussion of [CATransaction setCompletionBlock]
The completion block object that is guaranteed to be called (on the main thread) as soon as all animations subsequently added by this transaction group have completed (or have been removed.) If no animations are added before the current transaction group is committed (or the completion block is set to a different value,) the block will be invoked immediately.

CATransaction setCompletionBlock example.
static void setX(UIView *view, CGFloat x)
{
    CGRect frame = view.frame;
    frame.origin.x = x;
    view.frame = frame;
}

- (IBAction)startAnimation:(id)sender {
    label.text = @"Animation starting!";
    setX(redView, 0);
    setX(blueView, 0);
    [CATransaction begin]; {
        [CATransaction setCompletionBlock:^{
            label.text = @"Animation complete!";
        }];
        [UIView animateWithDuration:1 animations:^{
            setX(redView, 300);
        }];
        [UIView animateWithDuration:2 animations:^{
            setX(blueView, 300);
        }];
    } [CATransaction commit];
}

Example of [CATransaction setCompletionBlock].
[CATransaction begin];
[CATransaction setAnimationDuration:0.5];
[CATransaction setAnimationTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
[CATransaction setCompletionBlock:^{
    NSLog(@"blabla");
}];

// Create the CABasicAnimation using your existing code
CABasicAnimation *myPropertyAnim = [CABasicAnimation animationWithKeyPath:@"myProperty"];
// TODO: Setup animation range
myPropertyAnim.toValue = newValue;

// The CATransaction does not observe arbitrary properties so this fails:
//myLayer.myProperty = newValue;

// Add the CAAnimation subclass during the CATransaction
[myLayer addAnimation:myPropertyAnim forKey:@"myKey"];

[CATransaction commit];

CATransaction setCompletionBlock example.
[CATransaction begin]; {

    [CATransaction setCompletionBlock:^{
        // This block runs after any animations created before the call to
        // [CATransaction commit] below.  Specifically, if
        // doMethodOneWhichHasAnimation starts any animations, this block
        // will not run until those animations are finished.

        SchemeView *schemeView = [[SchemeView alloc] init];
            [self.navigationController pushViewController:schemeView animated:YES];
            [schemeView release];
    }];

    // You don't need to modify `doMethodOneWhichHasAnimation`.  Its animations are
    // automatically part of the current transaction.
    [otherClass doMethodOneWhichHasAnimation];

} [CATransaction commit];

End of CATransaction setCompletionBlock example article.