首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何在CAKeyframeAnimation完成时指定选择器?

如何在CAKeyframeAnimation完成时指定选择器?
EN

Stack Overflow用户
提问于 2010-03-18 15:37:31
回答 2查看 11.3K关注 0票数 21

我正在使用CAKeyframeAnimation沿CGPath为视图设置动画。当动画完成时,我希望能够调用其他方法来执行另一个操作。有什么好方法可以做到这一点吗?

我看过使用UIView的setAnimationDidStopSelector:,但是从文档中看,它似乎只适用于在UIView动画块(beginAnimations和commitAnimations)中使用。我也试了一下以防万一,但它似乎不起作用。

下面是一些示例代码(这是在一个自定义的UIView子类方法中):

代码语言:javascript
复制
// 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:,这应该是可行的,但似乎不是最好的方法。只要我设置的延迟等于动画的持续时间,那么它应该是好的。

有没有更好的方法?谢谢!

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2010-03-18 15:46:37

CAKeyframeAnimation是CAAnimation的一个子类。CAAnimation中有一个delegate property。委托可以实现-animationDidStop:finished: method。剩下的事情应该很简单。

票数 24
EN

Stack Overflow用户

发布于 2012-06-19 17:39:40

或者,您可以使用以下命令来封装动画:

代码语言:javascript
复制
[CATransaction begin];
[CATransaction setCompletionBlock:^{
                   /* what to do next */
               }];
/* your animation code */
[CATransaction commit];

并设置完成块来处理您需要做的事情。

票数 37
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/2468166

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档