zoukankan      html  css  js  c++  java
  • iOS项目 画图小程序

    两个重点 一个设置一个

    CGMutablePathRef 来存放不断变动的线条

    另一个

    @property (nonatomic,strong)NSMutableArray *paths;

     用来存放已经画好的线条

    代码如下

    #import "DSNView.h"

    #import "DSNPath.h"

    @interface DSNView ()

    @property (nonatomic,strong)NSMutableArray *paths;

    @end

    @implementation DSNView

    {

        CGPoint point;

        CGMutablePathRef path;

    }

    -(NSMutableArray *)paths

    {

        if (!_paths) {

            _paths = [NSMutableArray array];

        }

        return _paths;

    }

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

    {

        UITouch *touch = [touches anyObject];

        point = [touch locationInView:self];

        

        path = CGPathCreateMutable();

        CGPathMoveToPoint(path, NULL, point.x, point.y);

        

    }

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

    {

        UITouch *touch = [touches anyObject];

        point = [touch locationInView:self];

        

        [self setNeedsDisplay];

    }

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

    {

        DSNPath *mPath = [[DSNPath alloc]init];

        

        mPath.myPath = path;

        [self.paths addObject:mPath];

    }

    // Only override drawRect: if you perform custom drawing.

    // An empty implementation adversely affects performance during animation.

    - (void)drawRect:(CGRect)rect {

        CGContextRef context = UIGraphicsGetCurrentContext();

        

        CGPathAddLineToPoint(path, NULL, point.x, point.y);

        

        CGContextAddPath(context, path);

        

        CGContextStrokePath(context);

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

            DSNPath *mPath = self.paths[i];

            

    //        mPath.myPath = path;

            CGContextAddPath(context, mPath.myPath);

            CGContextStrokePath(context);

        }

    }

  • 相关阅读:
    类在编写过程中的一些注意事项
    SDUT 2893-B(DP || 记忆化搜索)
    Microsoft.AlphaImageLoader滤镜解说
    解决网页版权符号模糊不清
    oninput,onpropertychange,onchange的使用方法和差别
    VB.net数据库编程(03):一个SQLserver连接查询的简单样例
    使用WinSetupFromUSB来U盘安装windowsXP(不使用win PE系统)
    ActivityManager
    IP协议
    jni编译non-numeric second argument to `wordlist' function错误
  • 原文地址:https://www.cnblogs.com/adodo/p/5238631.html
Copyright © 2011-2022 走看看