【问题标题】:iPhone CALayer animationiPhone CALayer 动画
【发布时间】:2011-02-21 10:54:42
【问题描述】:

我正在绘制一条线动画,用一条线连接 2 个点。这是我的代码:

-(void) drawObject{     
    rootLayer   = [CALayer layer];
    rootLayer.frame = self.view.bounds;
    [self.view.layer addSublayer:rootLayer];

    lineStartPath = CGPathCreateMutable();
    CGPathMoveToPoint(lineStartPath, nil, 160, 50);
    CGPathAddLineToPoint(lineStartPath, nil, 160, 50);
    CGPathCloseSubpath(lineStartPath);

    lineEndPath = CGPathCreateMutable();
    CGPathMoveToPoint(lineEndPath, nil, 160, 50);
    CGPathAddLineToPoint(lineEndPath, nil, 160, 300);
    CGPathCloseSubpath(lineEndPath);

    shapeLayer = [CAShapeLayer layer];
    shapeLayer.path = lineStartPath;
    UIColor *strokeColor = [UIColor colorWithHue:0.557 saturation:0.55 brightness:0.96 alpha:1.0];
    shapeLayer.strokeColor = strokeColor.CGColor;
    shapeLayer.lineWidth = 3.0;

    [rootLayer addSublayer:shapeLayer];

    [self performSelector:@selector(startAnimation) withObject:nil afterDelay:1.0];
}

-(void) startAnimation{
    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"path"];
    animation.duration = 0.5;
    animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
    animation.fromValue = (id)lineStartPath;
    animation.toValue = (id)lineEndPath;
    [shapeLayer addAnimation:animation forKey:@"animatePath"];
}

弱点是动画结束后,线条消失了。我希望在这两点之间画线后,id 不会消失。请给我一个提示。

【问题讨论】:

    标签: iphone core-animation core-graphics quartz-graphics


    【解决方案1】:

    尝试添加

    shapeLayer.path = lineEndPath;
    

    startAnimation的开头。

    视频中对此进行了解释:Session 424 - Core Animation in Practice, Part 1 来自 WWDC 2010(大约 38:00)。

    并将[shapeLayer addAnimation:animation forKey:@"animatePath"]; 替换为[shapeLayer addAnimation:animation forKey:@"path"];

    【讨论】:

    • 添加动画时的key可以任意,仅用于使用[myLayer animationForKey:@"myKey"];取回动画
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-05
    • 2014-04-15
    • 2011-03-16
    • 1970-01-01
    相关资源
    最近更新 更多