cocoacompletionhandlernsanimationcontext

NSAnimationContext completion parameter?


My animation can be performed on several different similar views (managed in an array), but the completion handler does not take a parameter. How then can I tell at completion time which animation I am completing?

The following its used to animate the frame of one (of several) sectionViews. When the animation is finished, I need to be able to send a notice to the particular sectionView.

NSLog(@"Animation started");

[NSAnimationContext beginGrouping];
[[NSAnimationContext currentContext] setDuration:kAnimationDuration];
[[NSAnimationContext currentContext] setCompletionHandler:^{
    [self doStuff];

    NSLog(@"Animation complete - now we need to notify the correct sectionView");
}];

[[sectionView animator] setFrame:[self frameForSectionView:sectionView]];

[NSAnimationContext endGrouping];

Solution

  • Just reference sectionView in the completion handler block. Or if it's an instance variable, static, or global, then create a local variable outside of the block initialized with the current value and then reference that local variable in the block.

    The block will capture the value.