动画, 画板

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<UITouch *> *)touches{
UITouch * touch = [touches anyObject];
CGPoint curP = [touch locationInView:self];
return curP;
}

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

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

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 12,251评论 4 61
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 173,462评论 25 708
  • 在iOS中随处都可以看到绚丽的动画效果,实现这些动画的过程并不复杂,今天将带大家一窥iOS动画全貌。在这里你可以看...
    F麦子阅读 5,141评论 5 13
  • 说一说路途上的事情吧。每次旅途会遇到不同的人,不同的事,不同的风景。 第一次,出远门是刚上大学那会儿,大学在淄博,...
    路之南阅读 191评论 0 1
  • 10月25号 星期三 天气晴 昨天回到家有点累,想等孩子睡着了再写。没想到孩子睡着我也睡着了。现在有点空...
    蔺雨菲妈妈阅读 222评论 0 0