• iOS 绘图


    -(id)initWithFrame:(CGRect)frame

    {

        self=[super initWithFrame:frame];

        if (self) {

            //使用self. 表示调用了set方法 retain了一次

            self.lineArray=[NSMutableArray arrayWithCapacity:1];

        }

        return self;

    }

    -(void)drawRect:(CGRect)rect

    {

        //得到上下文,即配置信息

        CGContextRef context = UIGraphicsGetCurrentContext();

        //设置颜色

        CGContextSetStrokeColorWithColor(context, [UIColor redColor].CGColor);

        //画笔粗细

        CGContextSetLineWidth(context, 2.0);

        //拿到小数组

        for (int i=0; i<[_lineArray count]; i++) {

            NSMutableArray * pointArray = [_lineArray objectAtIndex:i];

            //每一笔的每个点连成线

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

                NSValue * fistPointvalue=[pointArray objectAtIndex:j];//第一个点

                NSValue * secondPointValue=[pointArray objectAtIndex:j+1];//的二个点

                

                //转出CGPoint 类型对象

                CGPoint firstPoint=[fistPointvalue CGPointValue];

                CGPoint secondPoint=[secondPointValue CGPointValue];

                

                //把笔触移动一个点

                CGContextMoveToPoint(context, firstPoint.x , firstPoint.y);

                

                //笔触和另一个点 连城一个路径

                CGContextAddLineToPoint(context, secondPoint.x, secondPoint.y);

                

            }

        }

        //执行绘制

        CGContextStrokePath(context);

        

        

    }

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

    {

        NSMutableArray *pointArray = [NSMutableArray arrayWithCapacity:1];

        //内层数组

        [_lineArray addObject:pointArray];

    }

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

    {

        UITouch *touch = [touches anyObject];

        CGPoint point = [touch locationInView:self];

         NSValue *pointValue=[NSValue valueWithCGPoint:point];

        

        NSLog(@"point=%@",NSStringFromCGPoint(point));

        //大数组

        NSMutableArray *pointArray=[_lineArray lastObject];

        

        [pointArray addObject:pointValue];

        

        //重绘

        [self setNeedsDisplay];

    }

    -(void)dealloc

    {

        [_lineArray release];

        [super dealloc];

    }

  • 相关阅读:
    Javascript模块化编程(一):模块的写法
    前端技术概括
    转:inline-block 前世今生
    把自己的代码添加到cocoapods 仓库;
    iOS 中获取系统的 相册以及相机的访问权限
    需要在进入下一级界面隐藏tabbar
    IOS框架研究之SDWebImage的原理以及使用流程
    NSLayoutConstraint
    cocoaPos 的安装和使用
    关于UItableView 分割线距离左边 15px 距离的问题 解决方案
  • 原文地址:https://www.cnblogs.com/iOS-mt/p/4090353.html
Copyright © 2020-2023  润新知