1 通过自定义view实现涂鸦,当点击换颜色时点击事件不生效,发现原因是没有设置自定义view的背景色为clearColor,
一定要加上这行代码: self.backgroundColor = [UIColor clearColor];
自定义view如下:
DrawPath.h
#import <UIKit/UIKit.h>
NS_ASSUME_NONNULL_BEGIN
@interface DrawPath : UIBezierPath
@property (nonatomic,strong) UIColor *pathColor;
@end
NS_ASSUME_NONNULL_END
DrawPath.m文件无代码
DrawPath.h 文件
#import <UIKit/UIKit.h>
#import "DrawPath.h"
NS_ASSUME_NONNULL_BEGIN
@interface DrawView : UIView
@property (nonatomic,strong) DrawPath *path;
@property (nonatomic,strong) NSMutableArray *pathArray;
@property (nonatomic,assign) float lineWidth;
@property (nonatomic,strong) UIColor *lineColor;
@property (nonatomic,strong) UIImage *image;
-(void)clear;
-(void)unDo;
-(void)setUp;
@end
NS_ASSUME_NONNULL_END
DrawView.m文件
#import "DrawView.h"
#import "LRessSourceMaterialCommon.h"
@implementation DrawView
#pragma mark - 初始化设置
- (void)setUp{
//给view添加一个是手势
UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc]init];
[panGesture addTarget:self action:@selector(pan:)];
[self addGestureRecognizer:panGesture];
self.backgroundColor = [UIColor clearColor];
// self.path.pathColor = [UIColor blackColor];
}
-(NSMutableArray *)pathArray{
if (_pathArray == nil)
{
_pathArray = [NSMutableArray array];
}
return _pathArray;
}
-(void)pan:(UIPanGestureRecognizer *)pan{
CGPoint currentP = [pan locationInView:self];
if (pan.state == UIGestureRecognizerStateBegan){
self.path = [[DrawPath alloc]init];
self.path.lineWidth = self.lineWidth;
self.path.pathColor = self.lineColor;
//起点
[self.path moveToPoint:currentP];
//保存描述好的路径
[self.pathArray addObject:self.path];
NSArray *numViews = self.superview.subviews;
for(int i=0;i<numViews.count;i++){
if([numViews[i] isKindOfClass:(UIButton.class)]){
UIButton *button = numViews[i];
NSInteger tagId=button.tag;
if(tagId==11){
[button setBackgroundImage:LRSourceMaterialImage(@"lr_source_cancel_s") forState:UIControlStateNormal];
[button setUserInteractionEnabled:YES];
}else if(tagId==12){
[button setBackgroundImage:LRSourceMaterialImage(@"lr_source_clear_s") forState:UIControlStateNormal];
[button setUserInteractionEnabled:YES];
}
}
}
}
[self.path addLineToPoint:currentP];
[self setNeedsDisplay];
}
#pragma mark-
-(void)drawRect:(CGRect)rect{
for (DrawPath *path in self.pathArray) {
if ([path isKindOfClass:[UIImage class]]){
//绘制图片
UIImage *image = (UIImage *)path;
[image drawInRect:rect];
}else{
//画线
[path.pathColor set];
[path stroke];
}
}
}
-(void)clear{
[self.pathArray removeAllObjects];
[self setNeedsDisplay];
}
-(void)unDo{
[self.pathArray removeLastObject];
[self setNeedsDisplay];
}
-(void)setImage:(UIImage *)image{
_image = image;
[self.pathArray addObject:_image];
[self setNeedsDisplay];
}
@end
在viewController里面调用如下:
@property (nonatomic, strong) DrawView *drawView;
- (DrawView *)drawView {
if(!_drawView){
_drawView = [[DrawView alloc]initWithFrame:self.view.frame];
_drawView.lineColor = MAHexColor(@"0XE75E58");
_drawView.lineWidth= kLRAdaptSize(4.5);
[_drawView setUp];
}
return _drawView;
}
[self.view addSubview:self.drawView];