iPhone核心动画 - 动画NSBezierPath

3

我想在屏幕上绘制路径时对其进行动画处理,我认为使用NSBezierPath和Core Animation是最好的方法,但我不确定如何实现。

基本上,想法是将从点A到点B的路径慢慢地从A到B进行动画处理,直到路径到达B。很棒的教程可以告诉我如何做到这一点。

1个回答

3
您可以使用您的CGPath(例如,可以从UIBezierPath创建)创建CAShapeLayer。
然后,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];

请问您能否提供一些示例代码来实现这个功能? - Oscar Gomez

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接