先搭界面,很简单的
#import "LSDrawView.h"
@interface LSDrawView()
@property(nonatomic,strong)UIBezierPath *path;
@end
@implementation LSDrawView
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
UITouch *touch = touches.anyObject;
CGPoint loc = [touch locationInView:touch.view];
UIBezierPath *path = [UIBezierPath bezierPath];
self.path = path;
[path moveToPoint:loc];
[self setNeedsDisplay];
}
-(void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
UITouch *touch = touches.anyObject;
CGPoint loc = [touch locationInView:touch.view];
[self.path addLineToPoint:loc];
[self setNeedsDisplay];
}
- (void)drawRect:(CGRect)rect {
[self.path stroke];
}
@end
画单线,渲染要在drawRect方法里,还有别忘记重绘
#import "LSDrawView.h"
@interface LSDrawView()
//@property(nonatomic,strong)UIBezierPath *path;
@property(nonatomic,strong)NSMutableArray *connectArray;
@end
@implementation LSDrawView
-(NSMutableArray *)connectArray{
if (!_connectArray) {
_connectArray = [NSMutableArray array];
}
return _connectArray;
}
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
UITouch *touch = touches.anyObject;
CGPoint loc = [touch locationInView:touch.view];
UIBezierPath *path = [UIBezierPath bezierPath];
// self.path = path;
[self.connectArray addObject:path];
[path moveToPoint:loc];
[self setNeedsDisplay];
}
-(void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
UITouch *touch = touches.anyObject;
CGPoint loc = [touch locationInView:touch.view];
// [self.path addLineToPoint:loc];
[self.connectArray.lastObject addLineToPoint:loc];
[self setNeedsDisplay];
}
- (void)drawRect:(CGRect)rect {
for (UIBezierPath *path in _connectArray) {
[path stroke];//统一渲染
}
}
@end
画多线,在原有的画单线的基础上进行修改,注意创建一个可变数组,初始化一下子,在渲染的时候进行统一渲染
#import "ViewController.h"
#import "LSDrawView.h"
@interface ViewController ()
@property (weak, nonatomic) IBOutlet LSDrawView *myDrawView;
@property (weak, nonatomic) IBOutlet UISlider *myslider;
@end
@implementation ViewController
- (IBAction)mySlider:(UISlider *)sender {
self.myDrawView.lineWidth = sender.value;
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.myDrawView.lineWidth = self.myslider.value;
[self.myDrawView setNeedsDisplay];
}
设置线宽,通过slider的值,拖线获取画板,设置。。。切记拖线要用UIslider,别用id,不然获取不到value值
#import "LSDrawView.h"
@interface LSDrawView()
@property(nonatomic,strong)NSMutableArray *connectArray;
@end
@implementation LSDrawView
-(NSMutableArray *)connectArray{
if (!_connectArray) {
_connectArray = [NSMutableArray array];
}
return _connectArray;
}
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
UITouch *touch = touches.anyObject;
CGPoint loc = [touch locationInView:touch.view];
UIBezierPath *path = [UIBezierPath bezierPath];
path.lineWidth = self.lineWidth;//正确
[self.connectArray addObject:path];
[path moveToPoint:loc];
[self setNeedsDisplay];
}
-(void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
UITouch *touch = touches.anyObject;
CGPoint loc = [touch locationInView:touch.view];
[self.connectArray.lastObject addLineToPoint:loc];
[self setNeedsDisplay];
}
- (void)drawRect:(CGRect)rect {
for (UIBezierPath *path in _connectArray) {
[path stroke];//统一渲染是不行的,容易一变多变
// path.lineWidth = self.lineWidth;
// [self setNeedsDisplay];//统一设置是不行的,容易一变多变
}
}
在这里渲染不能在drawRect方法里直接统一设置, 容易一变多变
需要给Path单独设置
设置颜色同理
path.lineColor = self.lineColor;//对颜色进行关联
[path.lineColor set];//设置颜色,单独设置颜色
关键性的两句代码
因为path中没有设置颜色的属性,所以,就自定义一个继承自UIbezierPath的类,然后声明一个UIcolor成员属性,跟画板里的path进行关联,最后在DrawRect方法里设置,注意程序是从上往下执行的所以,要将设置颜色放在渲染的上方不然会出现默认颜色延迟改变的情况
- (IBAction)colorButton:(UIButton *)sender {
self.myDrawView.lineColor = sender.backgroundColor;
}
- (IBAction)clear:(UIBarButtonItem *)sender {
[self.myDrawView.connectArray removeAllObjects];
[self.myDrawView setNeedsDisplay];
}
- (IBAction)back:(UIBarButtonItem *)sender {
[self.myDrawView.connectArray removeLastObject];
[self.myDrawView setNeedsDisplay];
}
- (IBAction)eraser:(UIBarButtonItem *)sender {
// LSBezierPath *path = [LSBezierPath bezierPath];
//
// self.myDrawView.lineColor = self.myDrawView.backgroundColor;
// [self.myDrawView.connectArray addObject:path];
//
// [self.myDrawView setNeedsDisplay];
self.myDrawView.lineColor = self.myDrawView.backgroundColor;
}
- (IBAction)save:(UIBarButtonItem *)sender {
UIGraphicsBeginImageContextWithOptions(self.myDrawView.frame.size, YES, 0.0);
CGContextRef ctx = UIGraphicsGetCurrentContext();
[self.myDrawView.layer renderInContext:ctx];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);
}
工具栏的改变,使用橡皮擦的注意事项,我们操作的是MyDrawView画板里的颜色属性,操作Path里的颜色属性是没有用的,除非self.MyDrawView.lineColor= path.lineColor,然后就可以操作path里的颜色属性了,可是看看不都一样吗?所以直接操作drawView里的颜色属性即可,将lineColor设置成画板的背景色,然后添加到存储路径的可变数组里,相当于制造一个假象哈哈