如何获取动画结束通知

New*_*bee 4 iphone xcode objective-c ios

动画结束后,我想做一些动作.

[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.80f];
self.view.transform = 
CGAffineTransformMakeTranslation(
                                 self.view.frame.origin.x, 
                                 480.0f + (self.view.frame.size.height/2)  // move the whole view offscreen
                                 );
[self.view setAlpha:0];
[UIView commitAnimations];
Run Code Online (Sandbox Code Playgroud)

我已经完成了上面的动画,如何找到动画结束,以便我可以在那之后做我的动作.

and*_*azz 7

用这个:

    [UIView animateWithDuration:0.80f animations:^{
    self.view.transform =
    CGAffineTransformMakeTranslation(
                                     self.view.frame.origin.x,
                                     480.0f + (self.view.frame.size.height/2)  // move the whole view offscreen
                                     );
    [self.view setAlpha:0];
    }
    completion:^(BOOL finished){
        // your code
    }];
Run Code Online (Sandbox Code Playgroud)


Joh*_*ith 7

将其添加到动画中:

[UIView setAnimationDidStopSelector:@selector(myAnimationEnded)];
[UIView setAnimationDelegate:self];
Run Code Online (Sandbox Code Playgroud)

这个方法会告诉你何时停止;

- (void)myAnimationEnded{
     NSLog(@"animation ended");
}
Run Code Online (Sandbox Code Playgroud)

  • 不要忘记`[UIView setAnimationDelegate:self];`. (2认同)
  • 我更喜欢这种设置动画属性的方法,我不喜欢在`{}`之间包装所有东西. (2认同)