简介
注意事项
values(NSArray *)
path(CGPathRef)
keyTimes(NSArray *)
timingFunctions(NSArray *)
calculationMode(NSString *)
rotationMode(NSString *)
效果图
实现思路
实现步骤(自定义UIView的子类)
@property (nonatomic, strong) UIBezierPath *path;
监听触摸事件的状态,绘制贝瑟尔曲线
//确定起点
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
//获取当前触摸点
UITouch *touch = [touches anyObject];
CGPoint curretnPoint = [touch locationInView:self];
//创建路径
UIBezierPath *path = [UIBezierPath bezierPath];
[path moveToPoint:curretnPoint];
//保存路径
self.path = path;
}
//添加线条
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
//获取当前触摸点
UITouch *touch = [touches anyObject];
CGPoint currentPoint = [touch locationInView:self];
//添加线条
[self.path addLineToPoint:currentPoint];
//重绘,将曲线显示到图层上
[self setNeedsDisplay];
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
//创建动画
CAKeyframeAnimation *animation = [CAKeyframeAnimation animation];
//指定执行动画的属性,
animation.keyPath = @"position";
//设置动画的执行路径
animation.path = self.path.CGPath;
//设置动画的执行时间
animation.duration = 1;
//设置动画的重复次数
animation.repeatCount = MAXFLOAT;
//将动画添加到对应的图层上
[[[self.subviews firstObject] layer] addAnimation:animation forKey:nil];
}
将路径显示到图层上
//绘制路径
- (void)drawRect:(CGRect)rect
{
[self.path stroke];
}