iPhone CALayer动画

iPhone CALayer动画,iphone,core-animation,core-graphics,quartz-graphics,Iphone,Core Animation,Core Graphics,Quartz Graphics,我正在绘制一个直线动画,用一条直线连接两个点。这是我的代码: -(void) drawObject{ rootLayer = [CALayer layer]; rootLayer.frame = self.view.bounds; [self.view.layer addSublayer:rootLayer]; lineStartPath = CGPathCreateMutable(); CGPathMoveToPoint(lineStar

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

-(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不会消失。请给我一个提示。

尝试添加

shapeLayer.path = lineEndPath;
开始动画的开头

这段视频对此进行了解释:来自WWDC 2010(大约38:00)


并替换
[shapeLayer addAnimation:animation forKey:@“animatePath”]
[shapeLayer addAnimation:animation forKey:@“path”]

添加动画时,关键点可以是任何您想要的,它仅用于使用
[myLayer animationForKey:@“myKey]”获取动画