2011-11-01 47 views
3

我想在屏幕上绘制路径时绘制动画,我假设Core Animation是NSBezierPath的最佳方式,但是我不确定如何实现此操作。iPhone核心动画 - 为NSBezierPath设置动画

基本上这个想法是有一条从A点到B点的路径,从A点到B点,直到路径达到B为止。一个关于如何做到这一点的好教程会很棒。

回答

3

您可以使用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]; 
+0

您能否提供一些示例代码来说明如何实现这一目标? –

+0

谢谢你为我完美工作。 –