我想在屏幕上绘制一条路径的动画,我假设核心动画是最好的方式,NSBezierPath
但是我不确定如何实现这一点。
基本上,这个想法是让一条从 A 点到 B 点的路径缓慢地从 A 到 B 动画,直到路径到达 B。关于如何做到这一点的好教程会很棒。
我想在屏幕上绘制一条路径的动画,我假设核心动画是最好的方式,NSBezierPath
但是我不确定如何实现这一点。
基本上,这个想法是让一条从 A 点到 B 点的路径缓慢地从 A 到 B 动画,直到路径到达 B。关于如何做到这一点的好教程会很棒。
您可以使用 CGPath 创建 CAShapeLayer(例如,可以从 UIBezierPath 创建)。
然后 CAShapeLayer 的 path 属性本身是可动画的,您还可以使用可动画的 strokeStart 和 strokeEnd 属性创建更高级的动画(从 iOS 4.2 开始可用)
如何添加与动画一起出现的随机线层的简单示例:
CAShapeLayer *l = [CAShapeLayer layer];
l.frame = self.view.bounds;
l.strokeColor = [UIColor redColor].CGColor;
CGPoint start = CGPointMake(arc4random()%300+10, arc4random()%400+40);
CGPoint end = CGPointMake(arc4random()%300+10, arc4random()%400+40);
UIBezierPath *path = [[UIBezierPath alloc] init];
[path moveToPoint:start];
[path addLineToPoint:end];
l.path = path.CGPath;
[path release];
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
animation.fromValue = [NSNumber numberWithFloat:0.0f];
animation.toValue = [NSNumber numberWithFloat:1.0f];
animation.duration = 3.0f;
[l addAnimation:animation forKey:@"myStroke"];
[self.view.layer addSublayer:l];