CAKeyframeAnimation完成后如何指定选择器?
我正在使用 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:。只要我将延迟设置为等于动画的持续时间,那么就应该没问题。
有更好的办法吗?谢谢!
I'm using a CAKeyframeAnimation to animate a view along a CGPath. When the animation is done, I'd like to be able to call some other method to perform another action. Is there a good way to do this?
I've looked at using UIView's setAnimationDidStopSelector:, however from the docs this looks like it only applies when used within a UIView animation block (beginAnimations and commitAnimations). I also gave it a try just in case, but it doesn't seem to work.
Here's some sample code (this is within a custom UIView sub-class method):
// 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"];
A workaround I was considering that should work, but doesn't seem like the best way, is to use NSObject's performSelector:withObject:afterDelay:. As long as I set the delay equal to the duration of the animation, then it should be fine.
Is there a better way? Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
或者您可以将动画包含在:
并设置完成块来处理您需要执行的操作。
Or you can enclose your animation with:
And set the completion block to handle what you need to do.
CAKeyframeAnimation 是 CAAnimation 的子类。有一个 CAAnimation 中的
委托
属性。委托可以实现-animationDidStop:finished:
方法。其余的应该很容易。CAKeyframeAnimation is a subclass of CAAnimation. There is a
delegate
property in CAAnimation. The delegate can implement the-animationDidStop:finished:
method. The rest should be easy.此答案的Swift 3语法。
Swift 3 syntax for this answer.