2011-11-01 83 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

謝謝你爲我完美工作。 –