【问题标题】:CAKeyframeAnimation not calling animationDidStop after bezier curve (iOS)CAKeyframeAnimation 在贝塞尔曲线之后没有调用 animationDidStop(iOS)
【发布时间】:2014-04-07 01:46:23
【问题描述】:

我已经查看了此处(以及整个网络)针对我的情况的所有其他答案,但尚未解决该问题。基本上正在发生的是我在二次贝塞尔曲线上为包含图像的 CALayer 设置动画。动画效果很好,但我真的需要实现动画后的行为,我所做的每一次尝试都失败了。这是我的代码:

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];

我正在尝试将动画的委托设置为 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"); 
}

我看不出我在做什么导致方法不被调用。根据我的理解(我对 iOS 编程相当陌生,当然),CAKeyFrameAnimation 动画 1 在停止动画时会自动将 animationDidStop 方法发送给其指定的委托,这正是我无法识别问题的原因。

我见过的所有其他解决方案或替代方案要么不起作用,要么建议使用块动画,为此我找不到在贝塞尔曲线上制作动画的方法。

【问题讨论】:

    标签: ios objective-c animation uibezierpath cakeyframeanimation


    【解决方案1】:

    在调用-[CALayer addAnimation:forKey:]之前设置动画的委托。

    注意CALayer.h中的评论:

    动画在添加到图层之前被复制,所以任何
    anim 的后续修改不会有任何影响,除非它是 添加到另一层。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-01-21
      • 1970-01-01
      • 2011-03-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多