绘图-简单手绘板的实现

</br>

前言

本文实现一个简单的手绘画板的效果,主要是记录下实现原理,虽然很简单,但是这可以是实现复杂效果的基础。毕竟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,下面是它的初始化方法

 + (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);
    }

小结

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

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • Quartz2D以及drawRect的重绘机制字数1487 阅读21 评论1 喜欢1一、什么是Quartz2D Q...
    PurpleWind阅读 4,262评论 0 3
  • 在iOS中随处都可以看到绚丽的动画效果,实现这些动画的过程并不复杂,今天将带大家一窥iOS动画全貌。在这里你可以看...
    F麦子阅读 10,541评论 5 13
  • 在iOS中随处都可以看到绚丽的动画效果,实现这些动画的过程并不复杂,今天将带大家一窥ios动画全貌。在这里你可以看...
    每天刷两次牙阅读 12,722评论 6 30
  • >复杂的组织都是专门化的 >Catharine R. Stimpson 到目前为止,我们已经探讨过`CALayer...
    夜空下最亮的亮点阅读 4,776评论 0 2
  • 今年是专职前端的第二年,从小白慢慢成长一个有点经验的工程师。14年开始转投前端,把玩Angular 跟Node.j...
    76b535312a75阅读 1,432评论 0 1

友情链接更多精彩内容