【问题标题】:how to track multitouch events on iphone?如何在 iPhone 上跟踪多点触控事件?
【发布时间】:2012-06-13 20:43:51
【问题描述】:

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

【问题讨论】:

    标签: iphone touch


    【解决方案1】:

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

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

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

    【讨论】:

      【解决方案2】:

      触摸对象在事件中是一致的,因此如果您想跟踪红色和蓝色触摸,您需要为每个触摸声明一个 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
          }
      }
      

      【讨论】:

      • 谢谢埃米利奥,我很感激! :)
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-01-12
      • 1970-01-01
      • 1970-01-01
      • 2014-02-28
      • 1970-01-01
      • 2013-05-28
      • 1970-01-01
      相关资源
      最近更新 更多