iOS

来源:互联网 发布:白种人为什么好看 知乎 编辑:程序博客网 时间:2024/06/05 17:20

本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 = [superinitWithFrame:frame];

    if (self) {


        _totalArray = [NSMutableArrayarray];


        [selfcreatUI];

    }

    returnself;

}

- (void)creatUI {


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

        

        UIButton *button = [UIButtonbuttonWithType:UIButtonTypeSystem];

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

        [button setTitleColor:[UIColorredColor] forState:UIControlStateNormal];

        button.backgroundColor = [UIColoryellowColor];

        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:selfaction:@selector(changePic:)forControlEvents:UIControlEventTouchUpInside];

        [selfaddSubview: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 = [_dataArrayfirstObject];

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

            [selfcustomSetLineWith:ref withArray:_dataArray];

          }else {

            // 滑动的历史

            NSArray *data = [_totalArrayobjectAtIndex:i];

            CustomModel *first = [dataobjectAtIndex:0];

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

            [selfcustomSetLineWith:ref withArray:data];

        }

    }

    CGContextStrokePath(ref);

}


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

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

        CustomModel *contentModel = [dataobjectAtIndex:j];

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

    }

}

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

    _dataArray = [NSMutableArrayarrayWithCapacity:0];

    [selfcustomGetThePointWithTouches:touches];

}


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

    [selfcustomGetThePointWithTouches:touches];

    [selfsetNeedsDisplay];

}


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

    UITouch *touch = [touchesanyObject];

    CGPoint point = [touchlocationInView:self];

    CustomModel *model = [[CustomModelalloc] init];

    model.px = point.x;

    model.py = point.y;

    [_dataArrayaddObject:model];

}

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

    [_totalArrayaddObject:_dataArray];

}


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

    [_dataArrayremoveAllObjects];

    switch (sender.tag) {

        caseBUTTON_TAG:

            [_totalArrayremoveAllObjects];

            break;

        caseBUTTON_TAG+1:

            [_totalArrayremoveLastObject];

            break;

        default:

            break;

    }

    [selfsetNeedsDisplay];

}



@end


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

//


#import "ViewController.h"

#import "CustomView.h"


@interface ViewController ()


@end


@implementation ViewController


- (void)viewDidLoad {

    [superviewDidLoad];

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

    

    [selfcreatView];

}


#pragma mark - 方法

- (void)creatView {

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

    view.backgroundColor = [UIColorcyanColor];

    [self.viewaddSubview:view];

}


到此结束




原创粉丝点击