iOS绘图

- (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
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值