如何在 iphone 上跟踪多点触控事件? [英] how to track multitouch events on iphone?

查看:23
本文介绍了如何在 iphone 上跟踪多点触控事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I would like to track the separate touch sequences from touchesBegan through touchesMoved up until the touchesEnded. I am getting the coordinates of single touch events, but I would like to know which touch event corresponds to which touch event sequence.

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

For instance, if I am moving a first finger across the screen, and then touch the screen with the second finger, and remove the first finger - I want to display the coordinates of the first finger in red color and the coordinates of second finger in blue color.

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

Is this possible? If yes, how do I determine which events should be "red" and which events should be "blue"?

这是我的代码:

-(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) {
        // ...
    }
}

推荐答案

触摸对象在事件中是一致的,因此如果您想跟踪红色和蓝色触摸,您需要为每个对象声明一个 iVar,当触摸开始,您将想要的触摸分配给该 ivar,然后在循环中检查触摸是否与您存储的指针相同.

The touch objects are consistent through the events, so if you wanted to track a red and a blue touches, you'd declare an iVar for each, when the touches start, you assign whichever touch you want to that ivar and then, in your loop, you would check if the touch is the same as the pointer you stored.

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

这篇关于如何在 iphone 上跟踪多点触控事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆