如何在iphone上跟踪多点触控事件?

joh*_*odo 1 iphone touch

我想从跟踪单独的触摸顺序touchesBegan通过touchesMoved,直到touchesEnded.我正在获取单点触摸事件的坐标,但我想知道哪个触摸事件对应于哪个触摸事件序列.

例如,如果我在屏幕上移动第一根手指,然后用第二根手指触摸屏幕,并移除第一根手指 - 我想显示第一根手指的红色坐标和第二根手指的坐标蓝色.

这可能吗?如果是,我如何确定哪些事件应为"红色"以及哪些事件应为"蓝色"?

这是我的代码:

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    [self handleTouches:[event allTouches]];
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    [self handleTouches:[event allTouches]];
}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    [self handleTouches:[event allTouches]];
}
-(void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
    [self handleTouches:[event allTouches]];
}

- (BOOL)handleTouches: (NSSet*)touches {
    for (UITouch* touch in touches) {
        // ...
    }
}
Run Code Online (Sandbox Code Playgroud)

Emi*_*aez 6

触摸对象在事件中是一致的,所以如果你想跟踪红色和蓝色触摸,你会为每个触摸声明一个iVar,当触摸开始时,你可以指定你想要的那个触摸,然后,在你的循环,您将检查触摸是否与您存储的指针相同.

UITouch *red;
UITouch *blue;

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    for (UITouch* touch in touches) {
        if(something) red = touch;
        else blue = touch;
    }
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    [self handleTouches:touches];
}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    for (UITouch* touch in touches) {
        if(red == touch) red = nil;
        if(blue == touch) blue = nil;
    }
}
-(void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
    for (UITouch* touch in touches) {
        if(red == touch) red = nil;
        if(blue == touch) blue = nil;
    }
}

- (BOOL)handleTouches: (NSSet*)touches {
    for (UITouch* touch in touches) {
        if(red == touch) //Do something
        if(blue == touch) //Do something else
    }
}
Run Code Online (Sandbox Code Playgroud)