用贝泽尔曲线描绘了一个路径。然后让CAKeyFrameAnimation的path是这个。就可以描绘出这个曲线路径了
CAKeyframeAnimation*keyFrameAnimation=[CAKeyframeAnimation animation];
keyFrameAnimation.keyPath=@"position";
keyFrameAnimation.duration=2;
//贝泽尔曲线描绘的路径
keyFrameAnimation.path=_bezierPath.CGPath;
keyFrameAnimation.repeatCount=MAXFLOAT;
[self.singleImageView.layer addAnimation:keyFrameAnimation forKey:nil];
#import "DrawView.h"
@interface DrawView()
@property (weak, nonatomic) IBOutlet UIImageView *singleImageView;
//@property (weak,nonatomic)UIImageView*imageView;U
@property(nonatomic,strong)UIBezierPath*bezierPath;
@end
@implementation DrawView
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
// Drawing code
}
*/
-(void)awakeFromNib{
[self addGestureRecognizer:[[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(panClick:)]];
}
-(void)panClick:(UIPanGestureRecognizer*)pan{
CGPoint currentP=[pan locationInView:self];
if (pan.state==UIGestureRecognizerStateBegan) {
_bezierPath=[UIBezierPath bezierPath];
[_bezierPath moveToPoint:currentP];
_bezierPath.lineWidth=5;
}else if(pan.state==UIGestureRecognizerStateChanged){
[_bezierPath addLineToPoint:currentP];
//每次添加线的时候 画一次
[self setNeedsDisplay];
}else if(pan.state==UIGestureRecognizerStateEnded){
CAKeyframeAnimation*keyFrameAnimation=[CAKeyframeAnimation animation];
keyFrameAnimation.keyPath=@"position";
keyFrameAnimation.duration=2;
//贝泽尔曲线描绘的路径
keyFrameAnimation.path=_bezierPath.CGPath;
keyFrameAnimation.repeatCount=MAXFLOAT;
[self.singleImageView.layer addAnimation:keyFrameAnimation forKey:nil];
}
// [self setNeedsDisplay];
}
-(void)drawRect:(CGRect)rect
{
[[UIColor redColor]setStroke];
[self.bezierPath stroke];
}