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];

    }

  • 相关阅读:
    那些年,我们一起做过的 Java 课后练习题(66 70)
    UI自动化测试:App的WebView页面中,当搜索栏无搜索按钮时处理方法
    追剧《大秦帝国》之感
    雷达距离方程 理解
    观影<和平战士> 之后感
    【转】使用blend改变图片颜色
    ios UITabBar/UITabBarController
    Windows批处理开启/停止服务及隐藏批处理窗口
    Windows设置定时自动重启
    Windows使用命令行查看文件的hash值(certutil)
  • 原文地址:https://www.cnblogs.com/liuwfuang96/p/4973726.html
Copyright © 2011-2022 走看看