如何跟踪iPhone的多点触控事件?(how to track multitouch events

2019-10-16 19:56发布

我希望跟踪从单独的触摸序列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) {
        // ...
    }
}

Answer 1:

触摸对象是通过活动相一致,因此,如果你想跟踪一个红色和蓝色的接触,你会宣布为每个伊娃,当触摸开始,你指定你想要的是伊娃取其触摸,然后在你的循环,你会检查是否触摸是一样的,你存储指针。

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
    }
}


Answer 2:

对于那些寻找跟踪多点触摸的通用解决方案,请参阅我的答案在这里 。

基本概念是每一个UITouch ID存储在数组中时touchesBegan::被调用,并且后来每个ID与屏幕上的触摸比较touchesMoved::事件。 这种方式,每一个手指可以与单个对象配对,和平移时沿着被跟踪。

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



文章来源: how to track multitouch events on iphone?
标签: iphone touch