IOS绘图

来源:互联网 发布:淘宝客服业绩软件 编辑:程序博客网 时间:2024/05/16 15:36
- (void)drawRect:(CGRect)rect{    //该方法提供给我们在视图显示之前自己绘制一些东西    //不需要创建画板        //获取当前的画板    CGContextRef context = UIGraphicsGetCurrentContext();    //设置画笔的颜色   // CGContextSetStrokeColorWithColor(context, <#CGColorRef color#>)    CGContextSetRGBStrokeColor(context, 1, 0, 0, 1);    //设置线条的宽度    CGContextSetLineWidth(context, 10);    //线条的样式    CGContextSetLineCap(context, kCGLineCapButt);    //把画笔移到某个点    if (allLinesArr.count == 0)    {        return;    }    for (NSMutableArray *smallArr in allLinesArr)    {        CGPoint firstPoint = [smallArr[0] CGPointValue];        CGContextMoveToPoint(context, firstPoint.x, firstPoint.y);        //跟另一个点连成一条线        for (int i = 1; i < smallArr.count; i++)        {            CGPoint nextPoint = [smallArr[i] CGPointValue];            CGContextAddLineToPoint(context, nextPoint.x, nextPoint.y);        }    }           //把画板上刚才描的路径画出来        //画线    CGContextStrokePath(context);        //画圆    //CGContextStrokeEllipseInRect(context, CGRectMake(150, 200, 200, 200));    //画矩形   // CGContextStrokeRect(context, CGRectMake(80, 220, 250, 250));}- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{    pointArr = [[NSMutableArray alloc]init];    UITouch *touch = [touches anyObject];    CGPoint beginPoint = [touch locationInView:self];    [pointArr addObject:[NSValue valueWithCGPoint:beginPoint]];    [allLinesArr addObject:pointArr];}- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{    UITouch *touch = [touches anyObject];    CGPoint movePoint = [touch locationInView:self];    [pointArr addObject:[NSValue valueWithCGPoint:movePoint]];    [self setNeedsDisplay];}- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{    }

0 0
原创粉丝点击