iPhone CALayer动画
我正在绘制一条线动画,用一条线连接 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 不会消失。请给我一个提示。
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, the line disappears. I want that after my line is drawn between those 2 points, id does not disappear. Give me a hint please.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试
在
startAnimation
的开头添加。此视频对此进行了解释:WWDC 的Session 424 - 核心动画实践,第 1 部分 2010 年(38:00 左右)。
并将[shapeLayer addAnimation:animation forKey:@"animatePath"];
替换为[shapeLayer addAnimation:animation forKey:@"path"];
。Try adding
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"];
.