为什么CAKeyframeAnimation在创建后没有出现?

时间:2015-10-27 14:50:24

标签: ios objective-c animation core-animation quartz-core

我的代码不起作用。

- (CAKeyframeAnimation  *)createAnimation {
    CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"path"];

    //does the @"path" mean the path I created below ? or path on some layer?

    animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];


    CGMutablePathRef path = CGPathCreateMutable();
    CGPathMoveToPoint(path, NULL, width-100, 0);

    CGPathAddQuadCurveToPoint(path, NULL, width+100, 300, width-100, height);
    CGPathAddQuadCurveToPoint(path, NULL, width+20, 220, width-100, height);
    CGPathAddQuadCurveToPoint(path, NULL, width+50, 230, width-100, height);
    // …… more code

    animation.path = path;
    animation.repeatCount = 10;
    animation.delegate = self;
    animation.duration = 2;

    CGPathRelease(path);
    return animation;
}

这是我的动画。我已经实施了委托animationDidStartdidStop。这个动画确实执行了。

然后我将此动画添加到自定义CALayer。我打算在图层上设置一条路径的动画。这是图层的代码:

 dragLayer = [[CAShapeLayer alloc] init];
    dragLayer.frame = CGRectMake(0, 0, width, height);
    dragLayer.path = [self pathToAnimateWithX:0 andY:0]; // custom method to create CGPathRef
    dragLayer.fillColor = appColor.CGColor;
    dragLayer.strokeColor = [UIColor redColor].CGColor  ;
    dragLayer.lineWidth = 5;
    [self.layer addSublayer:dragLayer];

    [dragLayer addAnimation:[self createAnimation] forKey:@"uposuuuition"];

我认为问题在于动画路径没有颜色,经过大量搜索后我不知道如何添加它(包括苹果的文档)。

另一个问题是如何为dragLayer自己的路径设置动画而不是重新创建它。

更新:我再次阅读了文档,CAKeyframeAnimation似乎是错误的方法。我找到了其他方法。感谢

1 个答案:

答案 0 :(得分:0)

问题在于,您正在为形状图层的path属性设置动画,但您希望为position属性设置动画。试试这个:

- (CAKeyframeAnimation  *)createAnimation {
    CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
    ...