支持多指觸屏

接受多指觸摸事件

self.multipleTouchEnabled = YES;

創(chuàng)建 Dictionary

touches 會返回多個觸摸事件,通過 NSMutableDictionary 來將 UITouch 與 Line 做關(guān)聯(lián)。

@property (nonatomic,strong)NSMutableDictionary *linesInProgress;

觸摸開始事件

遍歷 touches,每一個對象都新建一個 line 與其對應(yīng),并存入 linesInProgress 中。
獲取 key 如下:

 NSValue *key = [NSValue valueWithNonretainedObject:t];

利用這個去做 dictionary 的key。

valueWithNonretainedObject:t

valueWithNonretainedObject: 方法將 UITouch 對象的內(nèi)存地址封裝為 NSValue 對象。整個觸摸過程內(nèi)存地址不變,所以在此期間可以通過此 key 獲取到 line。

為什么 UITouch 不能作為 key?
鍵必須遵守 NSCopying 協(xié)議,可以相應(yīng) copy 消息。
UITouch 是唯一的,不能被復(fù)制。

最終代碼

#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<UITouch *> *)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<UITouch *> *)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<UITouch *> *)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

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 下次再見都將改變。 今天陪琳試婚紗和敬酒服,按耐不住自己內(nèi)心的公主心,也試了二套婚紗。 十分緊張,琳說給我拍張照留...
    丁小穎閱讀 354評論 7 1
  • 我懂得很多道理,對這一生曾有過好的把握。 參加艾力酷艾英語的全年團,從元旦開始按部就班的早起晨讀,在專用本上把老師...
    魏雨self閱讀 394評論 0 0

友情鏈接更多精彩內(nèi)容