【问题标题】:Why CAKeyframeAnimation didn't present after created?为什么 CAKeyframeAnimation 创建后没有出现?
【发布时间】:2015-10-27 14:50:24
【问题描述】:

我的代码不起作用。

- (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 似乎是错误的方法。我找到了其他方法。谢谢

【问题讨论】:

  • @manman 谢谢,它移动了。但我想为路径的形状设置动画,没有位置,我该怎么做
  • 你的意思是像this answer这样的东西吗?
  • 你能澄清你想要做什么吗?您是否需要为从一条路径到另一条路径的过渡设置动画,或者只是为路径的绘制设置动画,就好像它是用钢笔绘制的一样?
  • @NoahWitherspoon 我想创建一个像wave这样的拖动动画。所以我需要为弧线制作动画。

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


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-15
    相关资源
    最近更新 更多