触摸画

本文详细介绍了如何在iOS设备上利用Objective-C实现连续绘画,避免每次touchBegin时重新初始化画笔的方法,并通过实例展示了触摸开始、触摸移动和触摸结束的处理流程。
//实现可以连续绘画, 不让画笔每次touchBegin时都重新初始化
- (UIBezierPath *)path
{
    if (!_path) {
        _path = [UIBezierPath bezierPath];
    }
    return _path;
}

- (void)drawRect:(CGRect)rect{
    [[UIColor redColor] setStroke];
    [self.path stroke];
}
//触摸开始
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
//NSSet 和 NSArray区别
//NSSet 内容是无序,不可重复的,存储效率高
//NSArray  内容是有序,可以重复
//NSSet的用途之一就是把数组内容转化为NSSet,进行元素去重操作
//当触点之后一个时,touches中自由一个对象
//取出任意一个对象,即可获取到触点的坐标
    UITouch *touch = [touches anyObject];
    CGPoint location = [touch locationInView:self];
//把画笔移动到这个坐标,准备开始画画
//    _path = [UIBezierPath bezierPath];
    [self.path moveToPoint:location];
}
//触摸移动
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
    UITouch *touch = [touches anyObject];
    CGPoint location = [touch locationInView:self];
    [self.path addLineToPoint:location];
    [self setNeedsDisplay]; //重绘
}
//触摸的结束
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
   
}

转载于:https://www.cnblogs.com/hangman/p/5410852.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值