我正在使用CAKeyframeAnimation沿CGPath为视图设置动画。当动画完成时,我希望能够调用其他方法来执行另一个操作。有什么好方法可以做到这一点吗?
我看过使用UIView的setAnimationDidStopSelector:,但是从文档中看,它似乎只适用于在UIView动画块(beginAnimations和commitAnimations)中使用。我也试了一下以防万一,但它似乎不起作用。
下面是一些示例代码(这是在一个自定义的UIView子类方法中):
// These have no effect since they're not in a UIView Animation Block
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:@selector(animationDidStop:finished:context:)];
// Set up path movement
CAKeyframeAnimation *pathAnimation = [CAKeyframeAnimation animationWithKeyPath:@"path"];
pathAnimation.calculationMode = kCAAnimationPaced;
pathAnimation.fillMode = kCAFillModeForwards;
pathAnimation.removedOnCompletion = NO;
pathAnimation.duration = 1.0f;
CGMutablePathRef path = CGPathCreateMutable();
CGPathMoveToPoint(path, NULL, self.center.x, self.center.y);
// add all points to the path
for (NSValue* value in myPoints) {
CGPoint nextPoint = [value CGPointValue];
CGPathAddLineToPoint(path, NULL, nextPoint.x, nextPoint.y);
}
pathAnimation.path = path;
CGPathRelease(path);
[self.layer addAnimation:pathAnimation forKey:@"pathAnimation"];
我正在考虑的一个变通方法是使用NSObject的performSelector:withObject:afterDelay:,这应该是可行的,但似乎不是最好的方法。只要我设置的延迟等于动画的持续时间,那么它应该是好的。
有没有更好的方法?谢谢!
发布于 2010-03-18 15:46:37
CAKeyframeAnimation是CAAnimation的一个子类。CAAnimation中有一个delegate
property。委托可以实现-animationDidStop:finished:
method。剩下的事情应该很简单。
发布于 2012-06-19 17:39:40
或者,您可以使用以下命令来封装动画:
[CATransaction begin];
[CATransaction setCompletionBlock:^{
/* what to do next */
}];
/* your animation code */
[CATransaction commit];
并设置完成块来处理您需要做的事情。
https://stackoverflow.com/questions/2468166
复制相似问题