2011-04-02 139 views
3

任何人都可以請幫我解決這個問題嗎?我有nsarray線,我需要在一些UIView中一個接一個地寫下它們(我想爲手繪圖製作動畫)。在UIView中動畫繪製線條

+0

線條如何存儲在NSArray中? – Kalle 2011-04-03 08:00:00

回答

0

這個代碼動畫一個集合上的對象點。可以使用相同的想法爲您的線路,我假設是CGPath?

// 
// animate along a set of points 
// 
// NSLog(@"The content of movement1PointsArray is%@",movement1PointsArray);  
CGMutablePathRef touchPath1 = CGPathCreateMutable(); 
CGPoint touchPath1StartPoint = [[movement1PointsArray objectAtIndex:0] CGPointValue]; 
CGPathMoveToPoint(touchPath1,NULL,touchPath1StartPoint.x, touchPath1StartPoint.y); 

for (NSInteger p = 0; p < [movement1PointsArray count]; ++p) 
{ 
    CGPoint touchesPointOnPath = [[movement1PointsArray objectAtIndex:p] CGPointValue]; 
    CGPathAddLineToPoint(touchPath1, NULL,touchesPointOnPath.x,touchesPointOnPath.y); 
} 
CAKeyframeAnimation* touchPathAnimation1 = [CAKeyframeAnimation animationWithKeyPath:@"position"]; 
[touchPathAnimation1 setDuration: 1.0]; 
[touchPathAnimation1 setAutoreverses: NO]; 
touchPathAnimation1.removedOnCompletion = NO; 
touchPathAnimation1.fillMode = kCAFillModeForwards; 
[touchPathAnimation1 setPath: touchPath1]; 
CFRelease(touchPath1); 
[animationsArray addObject:touchPathAnimation1]; 
[ball.layer addAnimation: touchPathAnimation1 forKey: @"position"]; 
    我有麻煩的第二路徑動畫...
  • 不管我怎麼努力,它只是動畫的最後一條路徑。
5

下面是答案(這裏找到:http://soulwithmobiletechnology.blogspot.fr/2012/07/how-to-animate-line-draw.html

UIBezierPath *path = [UIBezierPath bezierPath]; 
[path moveToPoint:CGPointMake(50.0,0.0)]; 
[path addLineToPoint:CGPointMake(120.0, 600.0)]; 

CAShapeLayer *pathLayer = [CAShapeLayer layer]; 
pathLayer.frame = self.view.bounds; 
pathLayer.path = path.CGPath; 
pathLayer.strokeColor = [[UIColor redColor] CGColor]; 
pathLayer.fillColor = nil; 
pathLayer.lineWidth = 2.0f; 
pathLayer.lineJoin = kCALineJoinBevel; 

[self.view.layer addSublayer:pathLayer]; 

CABasicAnimation *pathAnimation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"]; 
pathAnimation.duration = 2.0; 
pathAnimation.fromValue = [NSNumber numberWithFloat:0.0f]; 
pathAnimation.toValue = [NSNumber numberWithFloat:1.0f]; 
[pathLayer addAnimation:pathAnimation forKey:@"strokeEnd"]; 

不要忘記#import <QuartzCore/QuartzCore.h>