6

I'm using animator to implement an animation, like

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

but I want to run a method or block after the animation finish, as follow:

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

Is there any way to implement it?

Stanislav Pankevich
  • 11,044
  • 8
  • 69
  • 129
NOrder
  • 2,483
  • 5
  • 26
  • 44
  • Maybe [this][1] can helps you, you should write a callback method. [1]: http://stackoverflow.com/questions/4824038/how-to-simplify-callback-logic-with-a-block – Mil0R3 Sep 03 '12 at 00:59

2 Answers2

13

You should use NSAnimationContext and it's completionHandler:

[NSAnimationContext beginGrouping];
[[NSAnimationContext currentContext] setCompletionHandler:^{
    NSLog(@"****");
}];
[[self.view animator] setFrame:newFrame];
[NSAnimationContext endGrouping];
Dmitry
  • 7,300
  • 6
  • 32
  • 55
  • 3
    I think you should set the completion handler before performing the animation. – Samir Sep 03 '12 at 08:08
  • @Samir can't find any confirmation in docs, but I think you're right. I've updated my answer. – Dmitry Sep 03 '12 at 08:28
  • @Dmitry check the doc [here](https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/NSAnimationContext_class/Introduction/Introduction.html). It has all what you need about dealing NSAnimationContext. – Samir Sep 03 '12 at 08:37
  • 1
    Confirming that if you don't set the completion handler **before** performing the animation (i.e. using _animator_) then your completion handler is called too early. – Borzh Jul 15 '15 at 20:55
1

I find another solution from WWDC video and hope the bellow code helps someone else

enter image description here

NOrder
  • 2,483
  • 5
  • 26
  • 44