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

    }

  • 相关阅读:
    web前端工程师
    java工程师
    原因原来默认预检测会检测是否存在多选框
    软件测试&安全测试高峰论坛
    安卓学习图
    为什么mongo中不能用int作为key
    历经小半宿吧。哎,终于搭建好了Linux-C的环境
    把昨晚写的东西完善了一下,还行,真差不多
    半宿了,仿写了个CList模板类,留着以后用吧
    今天复习了一下完成端口网络模型
  • 原文地址:https://www.cnblogs.com/iOS-mt/p/4090353.html
Copyright © 2011-2022 走看看