支持多指触屏

接受多指触摸事件

self.multipleTouchEnabled = YES;

创建 Dictionary

touches 会返回多个触摸事件,通过 NSMutableDictionary 来将 UITouch 与 Line 做关联。

@property (nonatomic,strong)NSMutableDictionary *linesInProgress;

触摸开始事件

遍历 touches,每一个对象都新建一个 line 与其对应,并存入 linesInProgress 中。
获取 key 如下:

 NSValue *key = [NSValue valueWithNonretainedObject:t];

利用这个去做 dictionary 的key。

valueWithNonretainedObject:t

valueWithNonretainedObject: 方法将 UITouch 对象的内存地址封装为 NSValue 对象。整个触摸过程内存地址不变,所以在此期间可以通过此 key 获取到 line。

为什么 UITouch 不能作为 key?
键必须遵守 NSCopying 协议,可以相应 copy 消息。
UITouch 是唯一的,不能被复制。

最终代码

#import "BNRDrawView.h"
#import "BNRLine.h"

@interface BNRDrawView()
//@property (nonatomic,strong)BNRLine *currentLine;
@property (nonatomic,strong)NSMutableDictionary *linesInProgress;
@property (nonatomic,strong)NSMutableArray *finishedLines;
@end

@implementation BNRDrawView
-(instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if(self){
        self.linesInProgress = [[NSMutableDictionary alloc]init];
        self.finishedLines = [[NSMutableArray alloc]init];
        self.backgroundColor = [UIColor grayColor];
        self.multipleTouchEnabled = YES;
    }
    return self;
}

-(void)strokeLine:(BNRLine *)line{
    UIBezierPath *bp = [UIBezierPath bezierPath];
    bp.lineWidth = 10;
    bp.lineCapStyle = kCGLineCapRound;
    [bp moveToPoint:line.begin];
    [bp addLineToPoint:line.end];
    [bp stroke];
    
}
-(void)drawRect:(CGRect)rect{
    [[UIColor blackColor]set];
    for(BNRLine *line in self.finishedLines){
        [self strokeLine:line];
    }
    [[UIColor redColor]set];
    for(NSValue *key in self.linesInProgress){
        [self strokeLine:self.linesInProgress[key]];
    }
//    if(self.linesInProgress ){
//        [[UIColor redColor]set];
//        [self strokeLine:self.currentLine];
//    }
//
}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    NSLog(@"%@",NSStringFromSelector(_cmd));
    for(UITouch *t in touches){
        CGPoint location = [t locationInView:self];
        BNRLine *line = [[BNRLine alloc]init];
        line.begin = location;
        line.end = location;
        NSValue *key = [NSValue valueWithNonretainedObject:t];
        self.linesInProgress[key] = line;
    }
//    UITouch *t = [touches anyObject];
//    CGPoint location = [t locationInView:self];
//    self.currentLine = [[BNRLine alloc]init];
//    self.currentLine.begin = location;
//    self.currentLine.end = location;
    [self setNeedsDisplay];
}

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    NSLog(@"%@",NSStringFromSelector(_cmd));
    for(UITouch *t in touches){
        NSValue *key = [NSValue valueWithNonretainedObject:t];
        BNRLine *line = self.linesInProgress[key];
        line.end = [t locationInView:self];
    }
//    UITouch *t = [touches anyObject];
//    CGPoint location = [t locationInView:self];
//    self.currentLine.end= location;
    [self setNeedsDisplay];
}

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
  
    NSLog(@"%@",NSStringFromSelector(_cmd));
    for(UITouch *t in touches){
        NSValue *key = [NSValue valueWithNonretainedObject:t];
        BNRLine *line = self.linesInProgress[key];
        [self.finishedLines addObject:line];
        [self.linesInProgress removeObjectForKey:key];
    }
//    self.currentLine = nil;
    [self setNeedsDisplay];
}

@end

你可能感兴趣的:(支持多指触屏)