UIPanGestureRecognizer获取所有触摸点的坐标。

3

我已添加了以下手势识别器:

UIPanGestureRecognizer *d2 = [[UIPanGestureRecognizer alloc] 
                              initWithTarget:self 
                              action:@selector(ViewDragging2:)];
[d2 setMinimumNumberOfTouches:2];
[d2 setMaximumNumberOfTouches:2];
[targetView addGestureRecognizer:d2];

当事件发生时触发的方法是:

-(void)ViewDragging2:(UIPanGestureRecognizer*)sender {

    // some point
    CGPoint translatedPoint = [(UIPanGestureRecognizer*)sender translationInView:targetView];
}

即使我用两根手指触摸,也能实现单点触控。如何获取第一和第二个触摸的坐标?
2个回答

8
您可以使用以下方法访问所有触摸:
  • (NSUInteger)numberOfTouches:返回当前触摸的数量。
  • (CGPoint)locationOfTouch:(NSUInteger)touchIndex inView:(UIView *)view:返回特定触摸在视图中的位置。
这些方法在基类UIGestureRecognizer中定义。

4

尝试下面的代码。

UIPanGestureRecognizer *d2 = [[UIPanGestureRecognizer alloc] 
                          initWithTarget:self 
                          action:@selector(ViewDragging2:)];
  [d2 setMinimumNumberOfTouches:2];
  [d2 setMaximumNumberOfTouches:2];
 [targetView addGestureRecognizer:d2];

当该事件发生时被触发的方法是:

   -(void)ViewDragging2:(UIPanGestureRecognizer*)sender
    {
      // where touchIndex is either 0 or 1.
       CGPoint location = [recognizer locationOfTouch:touchIndex inView:self.view];
   }

请查看此链接:locationOfTouch和numberOfTouches

祝好, 尼尔。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接