小画板demodemo

先搭界面,很简单的



#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设置成画板的背景色,然后添加到存储路径的可变数组里,相当于制造一个假象哈哈

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值