《绘画----涂鸦》ViewController绘制线。橡皮擦和截图功能。

效果图:




2、我们看代码

是ViewController 里面,绘制一条线。但是我们知道,绘制一条线,需要上下文。但是ViewController 不是View 。它没有上下文。那怎么办呢?。请看下面代码:

-(void)DrawLine{

    imageView=[[UIImageViewalloc] initWithFrame:CGRectMake(0,64, self.view.bounds.size.width,1)];

    [self.viewaddSubview:imageView];

    //获取上下文,获取上下文有两种情况。(从ViewImage上)

    UIGraphicsBeginImageContext(imageView.frame.size);

    // 在图像上画

    [imageView.imagedrawInRect:CGRectMake(0,0, imageView.frame.size.width,imageView.frame.size.height)];

    // 设置线条的样式

    CGContextSetLineCap(UIGraphicsGetCurrentContext(),kCGLineCapRound);

    //设置,线条的宽度

    CGContextSetLineWidth(UIGraphicsGetCurrentContext(),2.0);

    // 线

    CGContextSetAllowsAntialiasing(UIGraphicsGetCurrentContext(),YES);

    // 设置线条的颜色

    CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(),0.0, 0.0, 1.0,1.0);

    // 开始路径

    CGContextBeginPath(UIGraphicsGetCurrentContext());

    // 将画笔,移动到某个起始点

    CGContextMoveToPoint(UIGraphicsGetCurrentContext(),0,0);

    // 添加线条,路径

    CGContextAddLineToPoint(UIGraphicsGetCurrentContext(),self.view.bounds.size.width,0);

    // 开始绘制

    CGContextStrokePath(UIGraphicsGetCurrentContext());

    // 获取上下文中的image

    imageView.image=UIGraphicsGetImageFromCurrentImageContext();

    // 显示imageView

    UIGraphicsEndImageContext();

}


就是这么多。主要是通过一个图片,来获取上下文。现在有人感觉说:不就是一条线吗?置于写那么多代码吗?弄个View 加个背景不可以吗?。我说可以啊,很好。


3、我们要重写View的好几个方法

// 第一个

-(instancetype)initWithFrame:(CGRect)frame{

    if ([superinitWithFrame:frame]) {

        self.backgroundColor = [UIColorredColor];

        //初始化保存所有点的数组

        self.AllPointArray = [NSMutableArrayarrayWithCapacity:0];

        /*

         注意:这两种声明的方法。可能存在的问题。(下面的声明方式,有可能导致数组提前释放)

         _AllPointArray = [NSMutableArray arrayWithCapacity:0];


         */

        //初始化颜色字典

        self.ColorDict = [NSMutableDictionarydictionaryWithObjects:@[@(0.0),@(0.0),@(0.0)]forKeys:@[@"R",@"G",@"B"]];

        //初始化颜色控制值

        self.Change =YES;

        UIView * Ba_View = [[UIViewalloc]initWithFrame:CGRectMake(0, frame.size.height-50, frame.size.width,50)];

        Ba_View.backgroundColor = [UIColorwhiteColor];

        [self addSubview:Ba_View];

        self.ClearBtn = [UIButtonbuttonWithType:UIButtonTypeCustom];

        self.ClearBtn.frame =CGRectMake(frame.size.width-(frame.size.width-320)/5-80,5, 80, 40);

        [self.ClearBtnsetTitle:@"撤销"forState:UIControlStateNormal];

        [self.ClearBtn.layersetCornerRadius:6];

        [self.ClearBtn.layersetBorderWidth:0.5];

        [self.ClearBtnsetTitleColor:[UIColorblackColor] forState:UIControlStateNormal];

        [self.ClearBtnaddTarget:selfaction:@selector(ClearClick)forControlEvents:UIControlEventTouchUpInside];

        [Ba_View addSubview:self.ClearBtn];

        /***************************************************/

        self.JTBtn = [UIButtonbuttonWithType:UIButtonTypeCustom];

        self.JTBtn.frame =CGRectMake(frame.size.width-(frame.size.width-320)*2/5-160,5, 80, 40);

        [self.JTBtnsetTitle:@"截图"forState:UIControlStateNormal];

        [self.JTBtn.layersetCornerRadius:6];

        [self.JTBtn.layersetBorderWidth:0.5];

        [self.JTBtnsetTitleColor:[UIColorblackColor] forState:UIControlStateNormal];

        [self.JTBtnaddTarget:selfaction:@selector(JTClick)forControlEvents:UIControlEventTouchUpInside];

        [Ba_View addSubview:self.JTBtn];

        /***************************************************/

        self.ChangeColorBtn = [UIButtonbuttonWithType:UIButtonTypeCustom];

        self.ChangeColorBtn.frame =CGRectMake(2*(frame.size.width-320)/5+80,5, 80, 40);

        [self.ChangeColorBtnsetTitle:@"颜色"forState:UIControlStateNormal];

        [self.ChangeColorBtn.layersetCornerRadius:6];

        [self.ChangeColorBtn.layersetBorderWidth:0.5];

        [self.ChangeColorBtnsetTitleColor:[UIColorblackColor] forState:UIControlStateNormal];

        [self.ChangeColorBtnaddTarget:selfaction:@selector(ChangColorClick)forControlEvents:UIControlEventTouchUpInside];

        [Ba_View addSubview:self.ChangeColorBtn];

        /***************************************************/


        self.XPCBtn = [UIButtonbuttonWithType:UIButtonTypeCustom];

        self.XPCBtn.frame =CGRectMake((frame.size.width-320)/5,5, 80, 40);

        [self.XPCBtnsetImage:[UIImageimageNamed:@"u=2475226411,3641308895&fm=21&gp=0.jpg"]forState:UIControlStateNormal];

        self.XPCBtn.contentMode = UIViewContentModeScaleAspectFill;

        [self.XPCBtn.layersetCornerRadius:6];

        [self.XPCBtn.layersetBorderWidth:0.5];

        [self.XPCBtnsetTitleColor:[UIColorblackColor] forState:UIControlStateNormal];

        [self.XPCBtnaddTarget:selfaction:@selector(XPCClick)forControlEvents:UIControlEventTouchUpInside];

        [Ba_View addSubview:self.XPCBtn];

        //设置默认橡皮差不被使用

        self.XPCUse =NO;

    }

    return self;

}

主要就是,控件的初始化。

第二:手势

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

    if (self.XPCUse) {

        UITouch * pointtouch = [touches anyObject];

        CGPoint   pointA = [pointtouch locationInView:self];

        [self floatView:pointA];

    }else{

       //创建数组保存移动过的点

    NSMutableArray * PointArray = [NSMutableArrayarrayWithCapacity:0];

    //再将该数组,放入到所有笔的数组里面

    [_AllPointArray addObject:PointArray];

    }

}


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

    // 判断数组里面的个数

    //我们要获得你的手指对象

    UITouch * Touch = [touches anyObject];

    //获取你手指触摸的点

    CGPoint Point = [Touch locationInView:self];


    if (self.XPCUse) {

        [self floatView:Point];

    }else{

       //将获取的点,保存到数组里面

    /*

      由于数组里面,只能放对象不能放点,因此,将点转化为对像

     */

    NSValue * Value = [NSValuevalueWithCGPoint:Point];

    //获取存放点的数组

    [[_AllPointArraylastObject] addObject:Value];

    //保存后,我们就进行绘制。

    [selfsetNeedsDisplay];

    }

}



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

    // 进行清除

    [self.FloatViewremoveFromSuperview];

    // 保留对象

    self.FloatView =nil;

}



4、最重要的步骤是

- (void)drawRect:(CGRect)rect {

    // 绘制,我们使用上下文,获得画布

    CGContextRef  ContextRef =UIGraphicsGetCurrentContext();

    //设置画画笔的颜色

    CGContextSetLineWidth(ContextRef,2);

    //设置画笔的颜色

    CGContextSetStrokeColorWithColor(ContextRef, [UIColorcolorWithRed:[_ColorDict[@"R"]floatValue] green:[_ColorDict[@"G"]floatValue] blue:[_ColorDict[@"B"]floatValue] alpha:1].CGColor);

    //获取所有的点,编制成路径

    for (int i =0; i<_AllPointArray.count; i++) {

        //获取每一笔,所有点的路径

        NSArray * OnePointArray = _AllPointArray[i];

        /*

         

         */

        for (int j =0; j<(int)OnePointArray.count-1; j++) {

            // 获取起始点 (注意:数组里面,存放的是对象NSValue,请转化为点)

            CGPoint OnePoint = [OnePointArray[j]CGPointValue];

            //在获取你下一个点

            /*

               这里是不是,有问题,你看到了吗。 OnePointArray[j+1]它有问题。那就是你要注意数组越界。

             */

            CGPoint NextPoint = [OnePointArray[j+1]CGPointValue];

            //我们要将画笔,移动到起始点

            CGContextMoveToPoint(ContextRef, OnePoint.x, OnePoint.y);

            //创建画笔绘制路径

            CGContextAddLineToPoint(ContextRef, NextPoint.x, NextPoint.y);

            

        }

        //所有的路径,都设置好了。我们开始,绘制

        CGContextStrokePath(ContextRef);

    }

    

}


完整代码下载地址:

http://download.csdn.net/detail/zhoushuangjian511/9370422




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值