• 涂鸦画板


    #import "ViewController.h"
    
    #import "DrawView.h"
    
    @interface ViewController ()
    
    @end
    @implementation ViewController
    
    -(void)loadView{
    
        self.view = [[DrawView alloc]init];
    
        self.view.backgroundColor = [UIColor whiteColor];
    
    }
    
    - (void)viewDidLoad {
    
        [super viewDidLoad];    
    }
    
    @end
    
    ////////////////////////////////////////////////////////////
    
    #import "DrawView.h"
    
    @interface DrawView()
    
    @end
    
    @implementation DrawView
    
    - (instancetype)initWithFrame:(CGRect)frame
    {
        self = [super initWithFrame:frame];
        if (self) {
            _lineArray = [NSMutableArray array];
    
            UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    
            btn.frame = CGRectMake(100, 420, 100, 40);
    
            btn.backgroundColor=[UIColor yellowColor];
            
            [btn setTitle:@"撤销" forState:UIControlStateNormal];
    
            [btn addTarget:self action:@selector(undoDraw) forControlEvents:UIControlEventTouchUpInside];
    
            [self addSubview:btn];
        }
        return self;
    }
    
    //撤销操作
    
    -(void)undoDraw{
        [_lineArray removeLastObject];//删除最后一条线
        [self setNeedsDisplay];//让View重绘,自动重绘
    }
    
    //每次手指开始触摸,增加一个新的数组,数组记录划过的点,利用这些点绘画
    
    -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
         [_lineArray addObject:[NSMutableArray arrayWithCapacity:1]];
    }
    -(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
    
        UITouch *touche = [touches anyObject];
        CGPoint point = [touche locationInView:self];
    
        //数组里需要id类型
    
        NSValue *value = [NSValue valueWithCGPoint:point];
    
        //把value装到_lineArray的最后一个元素(数组)里
    
        [[_lineArray lastObject] addObject:value];
    
        [self setNeedsDisplay];//重绘界面
    
    }
     
    -(void)drawRect:(CGRect)rect{
     
        CGContextRef context = UIGraphicsGetCurrentContext();//获得上下文
    
        CGContextSetStrokeColorWithColor(context, [UIColor redColor].CGColor);//画笔颜色
    
        for (int i=0; i<[_lineArray count]; i++) {
    
            NSMutableArray *pointArray = [[NSMutableArray alloc]initWithArray:[_lineArray objectAtIndex:i]];
    
            for (int j=0; j<[pointArray count]-1; j++) {
    
                //遍历所有的点
    
                NSValue *a = [pointArray objectAtIndex:j];
    
                CGPoint p1 = [a CGPointValue];
    
                NSValue *b = [pointArray objectAtIndex:j+1];
    
                CGPoint p2 = [b CGPointValue];
    
    
                CGContextMoveToPoint(context, p1.x, p1.y);
    
                CGContextAddLineToPoint(context, p2.x, p2.y);
            }        
        }
    
        //在当前的图形上下文中,根据画笔的颜色和画笔的路径,进行绘制
    
        CGContextStrokePath(context);
    }
    
    @end
  • 相关阅读:
    set集合 浅层拷贝会和深层拷贝
    "is"与"=="
    元组和字典
    运算符和列表
    Python 基础语法
    supervisor 安装配置详解
    如何运行vue项目
    过目不忘JS正则表达式
    vue Bus总线
    Robot Framework 环境安装(一)
  • 原文地址:https://www.cnblogs.com/liuyingjie/p/5028063.html
Copyright © 2020-2023  润新知