//
// 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
画板画图
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 为什么管理人员都喜欢用Visio画图?其实有原因的: 在这里,我归纳为以下几点: 形状数据一体化,这是管理者最喜欢...
- 刚刚过去了春节,接下来接踵而来妇女节,劳动节,端午节,中秋节,国庆节。。。对的,你总要发各种节日图片,员工生日...
- 最通俗易懂的表达方式 文字需要翻译,图片不用。在图片的世界,不管是中国人、印度人、美国人、英国人的笑,全世界人都能...