iOS绘图功能

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    NSMutableArray *pointArray = [NSMutableArray arrayWithCapacity:1];
    [_lineArry addObject:pointArray];
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];
    // 获取当前点的位置
    CGPoint point = [touch locationInView:self];
    NSLog(@"point = %@", NSStringFromCGPoint(point));
    // 拿到最后一个对象
    NSMutableArray *pointArray = [_lineArry lastObject];
    // 转换成对象类型
    NSValue *pointValue = [NSValue valueWithCGPoint:point];
    // 添加到数组对象中
    [pointArray addObject:pointValue];
    
    [self setNeedsDisplay];
}
- (void)drawRect:(CGRect)rect
{
    // 拿到绘图的配置信息(绘画的属性信息)
    // 通过图片得到上下文
    CGContextRef context = UIGraphicsGetCurrentContext();
    // 设置画笔的颜色
    CGContextSetStrokeColorWithColor(context, [UIColor redColor].CGColor);
    // 设置画笔的粗细
    CGContextSetLineWidth(context, 2.0);
    for (int i = 0; i < [_lineArry count]; i++) {
        NSMutableArray *pointArry = [_lineArry objectAtIndex:i];
        for (int j = 0; j < (int)pointArry.count-1; j++) {
            NSValue *firstPointValue = [pointArry objectAtIndex:j];
            NSValue *secondPointValue = [pointArry objectAtIndex:j + 1];
            CGPoint firstPoint  =  [firstPointValue CGPointValue];
            CGPoint secondPoint = [secondPointValue CGPointValue];
            // 把笔触移动到一个点
            CGContextMoveToPoint(context, firstPoint.x, firstPoint.y);
            // 笔触和另一点形成一个路径(从move到point)
            CGContextAddLineToPoint(context, secondPoint.x, secondPoint.y);
        }
    }// 绘制图形
    CGContextStrokePath(context);
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值