zoukankan      html  css  js  c++  java
  • 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];

    }

  • 相关阅读:
    链表实现python list数据类型
    python图形图像处理--验证码的制作
    Java封装、继承和抽象的实例
    C#指定几所城市的天气预报
    我的第一个全站项目纪实
    activiti集成spring
    刷新token并继续之前的请求
    微信小程序开发准备
    zookeeper和dubbo
    zookeeper
  • 原文地址:https://www.cnblogs.com/iOS-mt/p/4090353.html
Copyright © 2011-2022 走看看