动画, 画板

动画, 画板_第1张图片
2016-08-20 11_25_22.gif
属性
/**贝塞尔曲线 */
@property (nonatomic, retain) UIBezierPath *bezier;
/**用来记录线的位置*/
@property (nonatomic, retain) NSMutableArray *beziers;
/** 子layer层*/
@property (nonatomic, retain) CALayer *subLayer;```
######自定义初始化

-(CALayer *)subLayer{
if (!_subLayer) {
self.subLayer = [[CALayer alloc] init];
self.subLayer.backgroundColor = [UIColor redColor].CGColor;
self.subLayer.bounds = CGRectMake(0, 0, 10, 10);
self.subLayer.cornerRadius = 5;
[self.layer addSublayer:_subLayer];
}
return _subLayer;
}
-(NSMutableArray *)beziers{
if (!_beziers) {
self.beziers = [NSMutableArray array]; }
return _beziers; }
-(CGPoint)getPointFromTouches:(NSSet *)touches{
UITouch * touch = [touches anyObject];
CGPoint curP = [touch locationInView:self];
return curP;
}

###### 想实现画图的功能
// 1 自定义View
// 2 实现touch相关的方法

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
CGPoint curp =
[self getPointFromTouches:touches];
// 绘图相关的类( UIBezierPath 贝塞尔曲线)
self.bezier = [UIBezierPath bezierPath];
// 开始点
[self.bezier moveToPoint:curp];
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{

CGPoint moveP = [self getPointFromTouches:touches];
[self.bezier addLineToPoint:moveP];
self.bezier.lineWidth = 5;
// 手动调用
[self setNeedsDisplay];
// 把每次的贝塞尔曲线添加到数组里
[self.beziers addObject:self.bezier];

}
-(void)start{
// 关键帧动画
CAKeyframeAnimation *animation = [CAKeyframeAnimation animation];
animation.keyPath = @"position";
// 动画的路径为贝塞尔曲线
animation.path = self.bezier.CGPath;
animation.repeatCount = MAXFLOAT;
animation.duration = 10;
// 将动画添加到子layer层
[self.subLayer addAnimation:animation forKey:nil];
// CAReplicatorLayer 复制图层
}

/** 在UIView上绘图的系统方法*/

-(void)drawRect:(CGRect)rect{
for (UIBezierPath *path in self.beziers) {
[path stroke];
}
}

你可能感兴趣的:(动画, 画板)