#import <QuartzCore/QuartzCore.h>
#import "ViewController.h"
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIImageView *imageView;
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
[self drawlineFromPoint:CGPointMake(10, 10) toPoint:CGPointMake(300, 300)];
}
- (void)drawlineFromPoint:(CGPoint)firstPoint toPoint:(CGPoint)secondPoint
{
//1. Create bezier path from first point to second.
UIBezierPath *path = [UIBezierPath bezierPath];
[path moveToPoint:firstPoint];
[path addLineToPoint:secondPoint];
//2. Create a shape layer for above created path.
CAShapeLayer *myLayer = [[CAShapeLayer alloc] init];
myLayer.strokeColor = [[UIColor greenColor] CGColor];
myLayer.lineWidth = 11.0;
myLayer.fillColor = nil;
myLayer.lineJoin = kCALineJoinBevel;
myLayer.path = path.CGPath;
[self.imageView.layer addSublayer:myLayer];
//3. Animate the path
CABasicAnimation *drawAnimation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
drawAnimation.duration = 3.0;
drawAnimation.fromValue = [NSNumber numberWithFloat:0.0f];
drawAnimation.toValue = [NSNumber numberWithFloat:1.0f];
drawAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn];
[myLayer addAnimation:drawAnimation forKey:@"drawLineAnimation"];
}
@end
Hope this helps!