简体   繁体   English

CAKeyframeAnimation在贝塞尔曲线(iOS)之后未调用animationDidStop

[英]CAKeyframeAnimation not calling animationDidStop after bezier curve (iOS)

I have looked at every other answer on here (and across the web) to a situation like mine and have yet to remedy the issue. 我已经在这里(和整个网络)查看了针对我的情况的所有其他答案,但尚未解决该问题。 Essentially what is occurring is I am animating an image-containing CALayer across a quadratic bezier curve. 本质上发生的是我正在使包含图像的CALayer跨二次贝塞尔曲线进行动画处理。 The animation works perfectly, but I really need to implement post-animation behavior, and every attempt I have made at doing so has failed. 动画效果完美,但是我确实需要实现动画后行为,而我为此所做的每一次尝试都失败了。 Here is my code: 这是我的代码:

UIBezierPath *path1 = [UIBezierPath bezierPath];
[path1 moveToPoint:P(67, 301)];
[path1 addQuadCurveToPoint:P(254, 301) controlPoint:P(160, 93)];

CALayer *ball1 = [CALayer layer];
ball1.bounds = CGRectMake(67, 301, 16.0, 16.0);
ball1.position = P(67, 301);
ball1.contents = (id)([UIImage imageNamed:@"turqouiseBallImage.png"].CGImage);
[self.view.layer addSublayer:ball1];

CAKeyframeAnimation *animation1 = [CAKeyframeAnimation animationWithKeyPath:@"position"];
animation1.path = path1.CGPath;
animation1.rotationMode = kCAAnimationRotateAuto;
animation1.repeatCount = 2.0;
animation1.duration = 5.0;
[ball1 addAnimation:animation1 forKey:@"ball1"];


[animation1 setDelegate:self];

I'm trying to set the delegate of the animation to self (my primary view controller; this is all in viewDidLoad), whose code implements the animationDidStop method: 我正在尝试将动画的委托设置为self(我的主视图控制器;这全部在viewDidLoad中),其代码实现了animationDidStop方法:

(interface) (only showing relevant code)
- (void)animationDidStop:(CAAnimation *)theAnimation finished:(BOOL)flag;

(implementation)
- (void)animationDidStop:(CAAnimation *)theAnimation finished:(BOOL)flag; {
NSLog(@"animation did stop"); 
}

I do not see what I am doing that is causing the method not to be called. 我看不到我在做什么导致该方法不被调用。 From my understanding (I am rather new to iOS programming, granted), the CAKeyFrameAnimation animation1 automatically sends the animationDidStop method to its designated delegate when it ceases to animate, which is precisely why I can't identify an issue. 根据我的理解(当然,我是iOS编程的新手),CAKeyFrameAnimation animation1在停止进行动画处理时会自动将animationDidStop方法发送给其指定的委托,这就是为什么我无法确定问题的原因。

Every other solution or alternative that I have seen has either not worked or suggested using block animation, for which I could not find a way to animate over a bezier curve. 我看到的所有其他解决方案或替代方案都无法使用或建议使用块动画,因此我无法找到在贝塞尔曲线上进行动画处理的方法。

Set the animation's delegate before calling -[CALayer addAnimation:forKey:] . 调用-[CALayer addAnimation:forKey:] 之前设置动画的委托。

Note the comment in CALayer.h : 注意CALayer.h的注释:

The animation is copied before being added to the layer, so any 在将动画添加到图层之前先对其进行复制,因此任何
subsequent modifications to anim will have no affect unless it is added to another layer. 除非对anim进行后续的修改,否则将其无效,除非将其添加到另一层。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM