Cocoa: run block after animation in OSX

I use an animator to implement animations like

[[self.view animator] setFrame:newFrame];

but I want to run a method or block after the animation is finished, as shown below:

[[self.view animator] setFrame:newFrame onComplete:^{
    NSLog(@"****");
}];

Is there any way to implement it?

+5
source share
2 answers

You should use NSAnimationContextand completionHandler:

[NSAnimationContext beginGrouping];
[[NSAnimationContext currentContext] setCompletionHandler:^{
    NSLog(@"****");
}];
[[self.view animator] setFrame:newFrame];
[NSAnimationContext endGrouping];
+11
source

I will find another solution from the WWDC video and hope that the following code will help someone else

enter image description here

+1
source

All Articles