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

时间:2012-06-10 19:35:57

标签: iphone touch

我想跟踪从touchesBegantouchesMoved直到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) {
        // ...
    }
}

2 个答案:

答案 0 :(得分:6)

触摸对象在事件中是一致的,所以如果你想跟踪红色和蓝色触摸,你会为每个触摸声明一个iVar,当触摸开始时,你指定你想要的那个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
    }
}

答案 1 :(得分:0)

对于那些寻找跟踪多个触摸的一般解决方案的人,请参阅我的回答here

基本概念是在调用touchesBegan::时将每个UITouch ID存储在一个数组中,然后将每个ID与touchesMoved::事件中屏幕上的触摸进行比较。这样,每个手指可以与单个对象配对,并在平移时跟踪。

通过这样做,跟踪触摸的每个对象都可以显示不同的颜色,然后在屏幕上显示以识别不同的手指。