简体   繁体   English

iPhone CALayer动画

[英]iPhone CALayer animation

I am drawing a line animation, connecting 2 points with a line. 我正在绘制一个线条动画,用一条线连接2个点。 This is my code: 这是我的代码:

-(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"];
}

The weak point is after the animation is ended, the line disappears. 弱点是在动画结束后,线条消失。 I want that after my line is drawn between those 2 points, id does not disappear. 我希望在我的线之间绘制这两点之后,id不会消失。 Give me a hint please. 请给我一个提示。

Try adding 尝试添加

shapeLayer.path = lineEndPath;

at the beginning of startAnimation . startAnimation开始时。

It's explained in this video: Session 424 - Core Animation in Practice, Part 1 from the WWDC 2010 (around 38:00). 在此视频中对此进行了解释: 会话424 - 实践中的核心动画, 2010年WWDC的第1部分 (大约38:00)。

and replace [shapeLayer addAnimation:animation forKey:@"animatePath"]; 并替换[shapeLayer addAnimation:animation forKey:@"animatePath"]; with [shapeLayer addAnimation:animation forKey:@"path"]; [shapeLayer addAnimation:animation forKey:@"path"]; .

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM