编写iOS应用时,可能需要知道某个动画会在何时结束。这样就可以在动画结束时执行特定的代码,例如执行下一个动画,或者更新某个对象。怎样才能知道某个动画已经结束了?可以为completion:传入一个Block对象。
修改BNRHypnosisViewController.m,在动画完成后向控制台输出一条日志信息。
[UIView animateKeyframesWithDuration:1.0 delay:0.0 options:0 animations:^{
[UIView addKeyframeWithRelativeStartTime:0 relativeDuration:0.8 animations:^{
messageLabel.center = self.view.center;
}];
[UIView addKeyframeWithRelativeStartTime:0.8 relativeDuration:0.2 animations:^{
int x = arc4random() % width;
int y = arc4random() % height;
messageLabel.center = CGPointMake(x, y);
}];
} completion:NULL];
[UIView animateKeyframesWithDuration:1.0 delay:0.0 options:0 animations:^{
[UIView addKeyframeWithRelativeStartTime:0 relativeDuration:0.8 animations:^{
messageLabel.center = self.view.center;
}];
[UIView addKeyframeWithRelativeStartTime:0.8 relativeDuration:0.2 animations:^{
int x = arc4random() % width;
int y = arc4random() % height;
messageLabel.center = CGPointMake(x, y);
}];
} completion:^(BOOL finished) {
NSLog(@“Animation finished”);
}];
UIInterpolatingMotionEffect *motionEffect =
[[UIInterpolatingMotionEffect alloc] initWithKeyPath:@“center.x”
type:UIInterpolatingMotionEffectTypeTiltAlongHorizontalAxis];
构建并运行应用。在动画完成后,控制台就会输出动画执行完成的日志信息。