ios开发实现画板功能

最近看了一些网上的画板demo,这些demo的实现方式基本上是使用CGContextRef或者UIBezierPath实现,但是基本上都存在一个比较严重的bug,在使用擦除功能的时候基本上都是直接将画板的颜色改为背景的颜色,那么当背景的是一张图片或者背景并不是单一颜色而是多种颜色时,擦除功能就会失效。本demo文章将解决这样一个问题。按照国际惯例先上图。

1.gif

demo主要使用CGContextRef实现,擦除功能使用kCGBlendModeDestinationIn和clearColor联合使用实现。
1、新建DWStroke类存储CGContextRef信息
DWStroke.h

#import <UIKit/UIKit.h>

typedef struct CGPath *CGMutablePathRef;
typedef enum CGBlendMode CGBlendMode;

@interface DWStroke : NSObject

@property (nonatomic) CGMutablePathRef path;
@property (nonatomic, assign) CGBlendMode blendMode;
@property (nonatomic, assign) CGFloat strokeWidth;
@property (nonatomic, strong) UIColor *lineColor;
- (void)strokeWithContext:(CGContextRef)context;
@end

DWStroke.m

- (void)strokeWithContext:(CGContextRef)context {
    CGContextSetStrokeColorWithColor(context, [_lineColor CGColor]);
    CGContextSetLineWidth(context, _strokeWidth);
    CGContextSetBlendMode(context, _blendMode);
    CGContextBeginPath(context);
    CGContextAddPath(context, _path);
    CGContextStrokePath(context);
}

2、画板
DrawTouchPointView.h

#import <UIKit/UIKit.h>
#import <QuartzCore/QuartzCore.h>

@interface DrawTouchPointView : UIView
/** 清屏 */
- (void)clearScreen;
/** 撤消操作 */
- (void)revokeScreen;
/** 擦除 */
- (void)eraseSreen;
/** 设置画笔颜色 */
- (void)setStrokeColor:(UIColor *)lineColor;
/** 设置画笔大小 */
- (void)setStrokeWidth:(CGFloat)lineWidth;
@end

DrawTouchPointView.m

@interface DrawTouchPointView () {
     CGMutablePathRef currentPath;//路径
}
//是否擦除
@property (nonatomic, assign) BOOL isEarse;
//存储所有的路径
@property (nonatomic, strong) NSMutableArray *stroks;
//画笔颜色
@property (nonatomic, strong) UIColor *lineColor;
//线条宽度
@property (nonatomic, assign) CGFloat lineWidth;
@end

初始化 ,背景颜色必须为[UIColor clearColor]

- (id)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        _stroks = [[NSMutableArray alloc] initWithCapacity:1];
        self.backgroundColor = [UIColor clearColor];
    }
    return self;
}

当用户点击画板存储路径的基本信息,并画起始点,根据使用擦除来设置blendMode属性和画笔的宽度,如果是擦除则blendMode = kCGBlendModeDestinationIn,画笔的大小为20,画笔颜色为[UIColor clearColor]

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    currentPath = CGPathCreateMutable();
    DWStroke *stroke = [[DWStroke alloc] init];
    stroke.path = currentPath;
    stroke.blendMode = _isEarse ? kCGBlendModeDestinationIn : kCGBlendModeNormal;
    stroke.strokeWidth = _isEarse ? 20.0 : _lineWidth;
    stroke.lineColor = _isEarse ? [UIColor clearColor] : _lineColor;
    [_stroks addObject:stroke];
    UITouch *touch = [touches anyObject];
    CGPoint point = [touch locationInView:self];
//    CGAffineTransform transform = CGAffineTransformMakeTranslation(0, 0);
//    CGPathAddArc(currentPath, &transform, point.x, point.y, _lineWidth/2.0, 0, 2*M_PI, 1);
    CGPathMoveToPoint(currentPath, NULL, point.x, point.y);
//    [self setNeedsDisplay];
}

开始移动, setNeedsDisplay 系统自动调用- (void)drawRect:(CGRect)rect

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [touches anyObject];
    CGPoint point = [touch locationInView:self];
    CGPathAddLineToPoint(currentPath, NULL, point.x, point.y);
    [self setNeedsDisplay];
}

在- (void)drawRect:(CGRect)rect中画图

- (void)drawRect:(CGRect)rect {
    CGContextRef context = UIGraphicsGetCurrentContext();
    for (DWStroke *stroke in _stroks) {
        [stroke strokeWithContext:context];
    }
}

清屏功能主要是移除数组中所有的路径并调用[self setNeedsDisplay]、设置_isEarse = NO
撤消功能主要是移除数组中最后一个对象并调用[self setNeedsDisplay]、设置_isEarse = NO
擦除功能只需修改isEarse属性为YES
画笔功能修改lineColor属性并设置_isEarse = NO
画笔大小功能修改strokeWidth属性并设置_isEarse = NO

/** 清屏 */
- (void)clearScreen {
    _isEarse = NO;
    [_stroks removeAllObjects];
    [self setNeedsDisplay];
}

/** 撤消操作 */
- (void)revokeScreen {
    _isEarse = NO;
    [_stroks removeLastObject];
    [self setNeedsDisplay];
}

/** 擦除 */
- (void)eraseSreen {
    self.isEarse = YES;
}
/** 设置画笔颜色 */
- (void)setStrokeColor:(UIColor *)lineColor {
    _isEarse = NO;
    self.lineColor = lineColor;
//    [self setNeedsDisplay];
}
/** 设置画笔大小 */
- (void)setStrokeWidth:(CGFloat)lineWidth {
    _isEarse = NO;
    self.lineWidth = lineWidth;
}
- (void)dealloc {
    CGPathRelease(currentPath);
}

打完收工,有什么问题欢迎指出!demo:http://code.cocoachina.com/view/135225
https://github.com/wuzaozhou/iOSDrawingBoard

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

推荐阅读更多精彩内容

  • 由于项目需求需要用到一个画板功能,需要这个画板可以实时的画,并且需要保存画板点集合从一端发送给另一端 达到一个实时...
    踏遍青山阅读 13,835评论 22 49
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 173,118评论 25 708
  • 朋友圈,是一个谁都可以抒发情感的空间。有发送者,有阅览者,形成了一个不用言语交流的方式。 许多人,起床、吃饭、闲暇...
    文颠阅读 592评论 0 0
  • 懂爱会爱做教育,懂教会教做教学 第三届“润木名师”教育教学系列培训
    润木泽林阅读 100评论 0 0
  • 固执的我,做了固执的决定。 以为逃离了自己厌恶了二十年的城市就可以重新开始。 天真的认为只有离开自己的人生才有转机...
    细细哒哒阅读 183评论 0 1