前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >绘图-简单手绘板的实现

绘图-简单手绘板的实现

作者头像
進无尽
发布2018-09-12 18:20:48
8520
发布2018-09-12 18:20:48
举报
文章被收录于专栏:進无尽的文章進无尽的文章
前言

本文实现一个简单的手绘画板的效果,主要是记录下实现原理,虽然很简单,但是这可以是实现复杂效果的基础。毕竟t他山之石可以攻玉。

手绘板.gif

原理思路
  • 在touchesBegan 方法中,每次都创建一个CAShapeLayer加载在当前视图的layer上,在touchesMoved方法中改变该 CAShapeLayer 基于UIBezierPath 的路径,即可实现绘制路径的效果。
  • 创建两个容器,lines用来盛放每次创建的CAShapeLayer,canceledLines 用来盛放每次删除的layer。
  • 清屏操作:移除当前视图layer上的所有子图层。并清空lines。
  • 撤销操作:移除当前视图layer上的lines最后的那个图层。并移除lines最后一个图层。把这个layer放入canceledLines中。
  • 恢复操作:当前视图layer加载上canceledLines中最后一个layer。移除canceledLines 中的最后一个layer,并加入到lines中。
源码实现

文件结构

自定义一个UIBezierPath的子类 LGPaintpath,下面是它的初始化方法

代码语言:javascript
复制
 + (instancetype)paintPathWithLineWidth:(CGFloat)width
                        startPoint:(CGPoint)startP
  {
    LGPaintPath * path = [[self alloc] init];
    path.lineWidth = width;
    path.lineCapStyle = kCGLineCapRound;
    path.lineJoinStyle = kCGLineCapRound; 
    [path moveToPoint:startP];
    return path;
}

LGDrawer中的实现

  • touchesBegan 事件 - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event { CGPoint startP = [self pointWithTouches:touches]; if ([event allTouches].count == 1) { LGPaintPath *path = [LGPaintPath paintPathWithLineWidth:_width startPoint:startP]; _path = path; CAShapeLayer * slayer = [CAShapeLayer layer]; slayer.path = path.CGPath; slayer.backgroundColor = [UIColor clearColor].CGColor; slayer.fillColor = [UIColor clearColor].CGColor; slayer.lineCap = kCALineCapRound; slayer.lineJoin = kCALineJoinRound; slayer.strokeColor = self.lineColor.CGColor; slayer.lineWidth = path.lineWidth; [self.layer addSublayer:slayer]; _slayer = slayer; [[self mutableArrayValueForKey:@"canceledLines"] removeAllObjects]; [[self mutableArrayValueForKey:@"lines"] addObject:_slayer]; } } - (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event { // 获取移动点 CGPoint moveP = [self pointWithTouches:touches]; if ([event allTouches].count == 1) { [_path addLineToPoint:moveP]; _slayer.path = _path.CGPath; } } - (CGPoint)pointWithTouches:(NSSet *)touches { UITouch *touch = [touches anyObject]; return [touch locationInView:self]; }
  • 清屏操作 - (void)clearScreen { if (!self.lines.count) return ; [self.lines makeObjectsPerformSelector:@selector(removeFromSuperlayer)]; [[self mutableArrayValueForKey:@"lines"] removeAllObjects]; [[self mutableArrayValueForKey:@"canceledLines"] removeAllObjects]; }
  • 撤销操作 - (void)undo { //当前屏幕已经清空,就不能撤销了 if (!self.lines.count) return; [[self mutableArrayValueForKey:@"canceledLines"] addObject:self.lines.lastObject]; [self.lines.lastObject removeFromSuperlayer]; [[self mutableArrayValueForKey:@"lines"] removeLastObject]; }
  • 恢复操作 - (void)redo { //当没有做过撤销操作,就不能恢复了 if (!self.canceledLines.count) return; [[self mutableArrayValueForKey:@"lines"] addObject:self.canceledLines.lastObject]; [[self mutableArrayValueForKey:@"canceledLines"] removeLastObject]; [self drawLine]; } // 画线 - (void)drawLine{ [self.layer addSublayer:self.lines.lastObject]; }
  • 截取某部分的视图 顺序不可以错 UIGraphicsBeginImageContext(_drawer.bounds.size); [_drawer.layer renderInContext:UIGraphicsGetCurrentContext()]; UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); //render的意思是渲染 //会使图片和背景图层在结合的地方更自然
  • 保存图片到相册中 - (void)loadImageFinished:(UIImage *)image{ UIImageWriteToSavedPhotosAlbum(image, self, @selector(image:didFinishSavingWithError:contextInfo:), (__bridge void *)self); } - (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo{ NSLog(@"image = %@, error = %@, contextInfo = %@", image, error, contextInfo); }
小结

这篇记录实现的手绘板效果,很简单,但是挺有趣。

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2017.06.16 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 前言
  • 原理思路
  • 源码实现
  • 小结
相关产品与服务
容器服务
腾讯云容器服务(Tencent Kubernetes Engine, TKE)基于原生 kubernetes 提供以容器为核心的、高度可扩展的高性能容器管理服务,覆盖 Serverless、边缘计算、分布式云等多种业务部署场景,业内首创单个集群兼容多种计算节点的容器资源管理模式。同时产品作为云原生 Finops 领先布道者,主导开源项目Crane,全面助力客户实现资源优化、成本控制。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档