在UIView中动画绘制线条

问题描述:

任何人都可以请帮我解决这个问题吗?我有nsarray线,我需要在一些UIView中一个接一个地写下它们(我想为手绘图制作动画)。在UIView中动画绘制线条

+0

线条如何存储在NSArray中? – Kalle 2011-04-03 08:00:00

您可能最终实现了视图drawRect方法。 这http://howtomakeiphoneapps.com/2009/08/how-to-draw-shapes-with-core-graphics/

应该让你开始

一些入口点,可以帮助:

这个代码动画一个集合上的对象点。可以使用相同的想法为您的线路,我假设是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"]; 
    我有麻烦的第二路径动画...
  • 不管我怎么努力,它只是动画的最后一条路径。

下面是答案(这里找到: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>