zoukankan      html  css  js  c++  java
  • [ios]quartz2d画板功功能实现核心代码

    //触摸开始

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

        

    //    1,获取对应的touch对象

        UITouch *touch = [touches anyObject];

    //    2,通过touch对象获取手指触摸对象

        CGPoint startPoint = [touch locationInView:touch.view];

    //    3,创建小数组,保存当前路径所有点

        NSMutableArray *subPoints = [NSMutableArray array];

    //    4,手指触摸对象起点存于数组

        [subPoints addObject:[NSValue valueWithCGPoint:startPoint]];

    //    5,小数组存入大数组

        [self.totalPoints addObject:subPoints];

    }

    //移动

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

        //    1,获取对应的touch对象

        UITouch *touch = [touches anyObject];

        //    2,通过touch对象获取手指触摸对象

        CGPoint movePoint = [touch locationInView:touch.view];

        //    3,从大数组中取出当前路径对应的小数组

        NSMutableArray *subPoints = [self.totalPoints lastObject];

        //    4,手指触摸对象起点存于数组

        [subPoints addObject:[NSValue valueWithCGPoint:movePoint]];

        //    5,调用drawRect方法重回视图

        [self setNeedsDisplay];

    }

    //触摸结束

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

        [self touchesMoved:touches withEvent:event];

    }

    //画图

    - (void)drawRect:(CGRect)rect {

    //    1.获取图形上下文

        CGContextRef ctx = UIGraphicsGetCurrentContext();

    //    2.遍历大数组取出小数组

        for(NSMutableArray *subPointArray in self.totalPoints)

        {

            for (int index = 0 ; index < subPointArray.count ; index++)

            {

    //            3.1取出小数组

                CGPoint point = [subPointArray[index] CGPointValue];

    //            3.2绘制线段

                if(0 == index){

    //                绘制起点

                    CGContextMoveToPoint(ctx, point.x, point.y);

                }else{

    //                绘制终点

                    CGContextAddLineToPoint(ctx, point.x, point.y);

                }

            }

        }

        CGContextSetLineCap(ctx, kCGLineCapRound);

        CGContextSetLineJoin(ctx, kCGLineJoinRound);

        CGContextSetLineWidth(ctx, 10);

    //    渲染

        CGContextStrokePath(ctx);

        

    }

    //  清屏

    -(void)clean

    {

        [self.totalPoints removeAllObjects];

        [self setNeedsDisplay];

    }

    //  撤销

    -(void)back

    {

        [self.totalPoints removeLastObject];

        [self setNeedsDisplay];

    }

  • 相关阅读:
    web页面常用方法及INI文件的读取方法
    winform 三个Panel左右切换(panel里面填充图片)
    图片渐出轮播的效果
    Winform跑马灯——Graphics运用
    .net 3.5 新功能重写ToInt()方法
    style.display
    SQL: 分页SQL SQL2005函数分页!
    JS: 验证输入必须为数字
    Table 里面点标题会进行排序
    在Div中绑定数据
  • 原文地址:https://www.cnblogs.com/liuwfuang96/p/4973726.html
Copyright © 2011-2022 走看看