iOS - 利用 DrawRect 实现简易画板功能的实现

本demo只实现基础画板涂鸦功能,撤销清屏功能,其余为空。demo框架如下:



#import <Foundation/Foundation.h>


@interface CustomModel : NSObject



// 中心点横坐标

@property (nonatomic,assign) float px;


// 中心点纵坐标

@property (nonatomic,assign) float py;



@end


//  Copyright © 2017 坏先森. All rights reserved.

//

#define  BUTTON_TAG  1000


#import "CustomView.h"

#import "CustomModel.h"


@interface CustomView () {

    

    float _pointX;

    

    float _pointY;

    

    NSMutableArray *_dataArray; // 每次开始触摸时候,存放的一组点

    

    NSMutableArray *_totalArray;  // 所有点的数组


}


@end


@implementation CustomView


- (instancetype)initWithFrame:(CGRect)frame

{

    self = [super initWithFrame:frame];

    if (self) {


        _totalArray = [NSMutableArray array];


        [self creatUI];

    }

    return self;

}

- (void)creatUI {


    for (NSInteger i = 0; i<2; i++) {

        

        UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];

        [button setTitle:i == 0 ? @"清空" : @"撤销" forState:UIControlStateNormal];

        [button setTitleColor:[UIColor redColor] forState:UIControlStateNormal];

        button.backgroundColor = [UIColor yellowColor];

        button.frame = CGRectMake(20+(80+self.frame.size.width-160-40)*(i%2), self.frame.size.height-50, 80, 40);

        button.tag = BUTTON_TAG + i;

        [button addTarget:self action:@selector(changePic:) forControlEvents:UIControlEventTouchUpInside];

        [self addSubview:button];

    }

}

// Only override drawRect: if you perform custom drawing.

// An empty implementation adversely affects performance during animation.

- (void)drawRect:(CGRect)rect {

    // 设置颜色宽度等

    CGContextRef ref = UIGraphicsGetCurrentContext();

    CGContextSetRGBStrokeColor(ref, 0.5, 0.5,0.5, 1);

    CGContextSetLineWidth(ref, 2);

  

    for (NSInteger i = 0; i<_totalArray.count+1; i++) {

        

        if (i == _totalArray.count) {

            // 当前正在触摸,滑动的部分

            // 这样处理

            // 找到起始点,数组里面的第一个元素

            CustomModel *lastFirst = [_dataArray firstObject];

            CGContextMoveToPoint(ref, lastFirst.px, lastFirst.py);

            [self customSetLineWith:ref withArray:_dataArray];

          }else {

            // 滑动的历史

            NSArray *data = [_totalArray objectAtIndex:i];

            CustomModel *first = [data objectAtIndex:0];

            CGContextMoveToPoint(ref, first.px, first.py);

            [self customSetLineWith:ref withArray:data];

        }

    }

    CGContextStrokePath(ref);

}


- (void)customSetLineWith:(CGContextRef)ref withArray:(NSArray *)data{

    for (NSInteger j = 0; j<data.count; j++) {

        CustomModel *contentModel = [data objectAtIndex:j];

        CGContextAddLineToPoint(ref, contentModel.px, contentModel.py);

    }

}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {

    _dataArray = [NSMutableArray arrayWithCapacity:0];

    [self customGetThePointWithTouches:touches];

}


- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {

    [self customGetThePointWithTouches:touches];

    [self setNeedsDisplay];

}


- (void)customGetThePointWithTouches:(NSSet<UITouch *> *)touches {

    UITouch *touch = [touches anyObject];

    CGPoint point = [touch locationInView:self];

    CustomModel *model = [[CustomModel alloc] init];

    model.px = point.x;

    model.py = point.y;

    [_dataArray addObject:model];

}

- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {

    [_totalArray addObject:_dataArray];

}


- (void)changePic:(UIButton *)sender {

    [_dataArray removeAllObjects];

    switch (sender.tag) {

        case BUTTON_TAG:

            [_totalArray removeAllObjects];

            break;

        case BUTTON_TAG+1:

            [_totalArray removeLastObject];

            break;

        default:

            break;

    }

    [self setNeedsDisplay];

}



@end


//  Copyright © 2017 坏先森. All rights reserved.

//


#import "ViewController.h"

#import "CustomView.h"


@interface ViewController ()


@end


@implementation ViewController


- (void)viewDidLoad {

    [super viewDidLoad];

    // Do any additional setup after loading the view, typically from a nib.

    

    [self creatView];

}


#pragma mark - 方法

- (void)creatView {

    CustomView *view = [[CustomView alloc] initWithFrame:self.view.frame];

    view.backgroundColor = [UIColor cyanColor];

    [self.view addSubview:view];

}


到此结束




猜你喜欢

转载自blog.csdn.net/q_q33757152/article/details/72676696
今日推荐