美文网首页
画板画图

画板画图

作者: Coder东 | 来源:发表于2017-04-17 16:36 被阅读34次
    //
    //  DrawboardView.m
    //  画板画图
    //
    //  Created by 品德信息 on 2017/4/17.
    //  Copyright © 2017年 品德信息. All rights reserved.
    //

#import "DrawboardView.h"

@interface DrawboardView ()
//is use eraser 使用橡皮擦
@property (nonatomic, assign) BOOL isEraserEnabled;

//pan color set
@property (nonatomic, strong) UIColor *panColor;
//pan line width

@property (nonatomic, assign) CGFloat panLineWidth;

@property (nonatomic, assign) CGFloat eraserLineWidth;
@property (nonatomic, strong) CAShapeLayer *currentDrawLayer;
@property (nonatomic, strong) UIBezierPath *currentDrawPath;
@property (nonatomic, strong) NSMutableArray *drawLayerArray;

@end

@implementation DrawboardView

//FIXME: MARK --lief cycle ---

- (instancetype)init {
if (self = [super init]) {
    self.backgroundColor = [UIColor whiteColor];
    self.isEraserEnabled = false;
    self.panColor = [UIColor blackColor];
    self.panLineWidth = 2.f;
    self.eraserLineWidth = 10.f;
}
return self;
}

-(instancetype)initWithFrame:(CGRect)frame{
if (self = [super initWithFrame:frame]) {
    self.backgroundColor = [UIColor whiteColor];
    self.isEraserEnabled = false;
    self.panColor = [UIColor blackColor];
    self.panLineWidth = 2.f;
    self.eraserLineWidth = 10.f;
}
return  self;
}

- (void)dealloc{
[self releaseAllLayers];
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {

UITouch *touch = touches.anyObject;
CGPoint startPoint = [touch locationInView: self];

self.currentDrawLayer = [self makeDrawLayer:self.isEraserEnabled];
self.currentDrawPath = [self makeDrawPath];

[self.currentDrawPath moveToPoint:startPoint];
}

-(void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{

UITouch *touch = touches.anyObject;
CGPoint currentPoint = [touch locationInView:self];

[self.currentDrawPath addLineToPoint:currentPoint];
self.currentDrawLayer.path  = self.currentDrawPath.CGPath;
}
-(NSMutableArray *)drawLayerArray {

if (!_drawLayerArray) {
    _drawLayerArray = [NSMutableArray arrayWithCapacity:10];
}
return _drawLayerArray;
}

#pragma mark -- Custom Methods -- 
-(CAShapeLayer *)makeDrawLayer:(BOOL)isEraserEnable {

CAShapeLayer *drawLayer = [CAShapeLayer layer];
drawLayer.frame = self.bounds;
drawLayer.fillColor = [UIColor clearColor].CGColor;
//擦除
if (!isEraserEnable) {
    drawLayer.lineWidth = self.panLineWidth;
    drawLayer.strokeColor = self.panColor.CGColor;
    
}else{
    drawLayer.lineWidth = self.eraserLineWidth;
    drawLayer.strokeColor = self.backgroundColor.CGColor;
}

[self.layer insertSublayer:drawLayer atIndex:(unsigned)self.drawLayerArray.count];
[self.drawLayerArray addObject:drawLayer];

return drawLayer;
}
-(UIBezierPath *)makeDrawPath{

UIBezierPath *drawPath = [UIBezierPath bezierPath];
drawPath.lineCapStyle = kCGLineCapRound;
drawPath.lineJoinStyle = kCGLineJoinRound;

return drawPath;
}

- (void)releaseAllLayers {

[self.drawLayerArray makeObjectsPerformSelector:@selector(removeFromSuperlayer)];
[self.drawLayerArray removeAllObjects];
}
@end

相关文章

  • opencv python版-lesson 06

    画板画图

  • 画板画图

  • 画图3

    画板画图 { UIImageView *_canvasImageView; //起点 CGPoint _start...

  • 2018-03-18 图片处理(二)----图片缩放

    笔记如下 实现步骤:1.首先动态获取图片2.画图片 1.准备画纸(大小参照原图) 2.准备画板,将画纸放到画板上 ...

  • 好用的软件

    1.Deskscribble,mac下的桌面画板 2.OmniGraffle,mac下画图软件。 3.MindMa...

  • 画图4

    简易画板画图 { //画布用于显示的图层 CAShapeLayer *_layer; //用于记录移动点路径 UI...

  • HTML + CSS 实现画图板

    背景介绍 基本介绍数位画图板:简称数位板、画图板、绘图板、绘画板、手绘板等等,是计算机输入设备的一种,数位画图板同...

  • iOS画板上画图片补充

  • iOS 签名画板

    一. 封装画图板 画板就是继承于UiView的一个子类,我们可以在这个子类中添加我们画图板相应的属性和方法,然后实...

  • 欧卡牌联想之十二

    李红丽 焦点网络初七 坚持分享第38天 我看到画面上有一个人在画板上画画,他正在用一只铅笔在白纸上画图,画板的上方...

网友评论

      本文标题:画板画图

      本文链接:https://www.haomeiwen.com/subject/twitzttx.html