iPhone CALayer animation
I am drawing a line animation, connecting 2 points with a line. 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, th开发者_运维知识库e line disappears. I want that after my line is drawn between those 2 points, id does not disappear. Give me a hint please.
Try adding
shapeLayer.path = lineEndPath;
at the beginning of startAnimation
.
It's explained in this video: Session 424 - Core Animation in Practice, Part 1 from the WWDC 2010 (around 38:00).
and replace [shapeLayer addAnimation:animation forKey:@"animatePath"];
with [shapeLayer addAnimation:animation forKey:@"path"];
.
精彩评论