Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to draw line with animation in iPhone

I am drawing line in iphone using CGContextRef. Can any one suggest me how i draw line with animation in iphone.

Please suggest.

like image 365
Mitesh Khatri Avatar asked Dec 03 '22 00:12

Mitesh Khatri


1 Answers

Here is the answer (found here : 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"];

Do not forget to #import <QuartzCore/QuartzCore.h>.

like image 141
Martin Avatar answered Dec 17 '22 13:12

Martin