1

我想跟踪从touchesBegan一直touchesMovedtouchesEnded. 我正在获取单次触摸事件的坐标,但我想知道哪个触摸事件对应于哪个触摸事件序列

例如,如果我在屏幕上移动食指,然后用食指触摸屏幕,然后移开食指 - 我想用红色显示食指的坐标,用红色显示食指的坐标蓝色。

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

这是我的代码:

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

2 回答 2

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
    }
}
于 2012-06-10T20:11:14.827 回答
0

对于那些正在寻找跟踪多次触摸的通用解决方案的人,请参阅我的答案

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

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

于 2017-04-16T08:30:05.477 回答