55

我想从 UIGestureRecognizer 获取我的水龙头的 UITouch 位置,但我无法通过查看文档和其他 SO 问题来弄清楚如何。你们中的一个可以指导我吗?

- (void)handleTap:(UITapGestureRecognizer *)tapRecognizer
{
    CCLOG(@"Single tap");
    UITouch *locationOfTap = tapRecognizer; //This doesn't work

    CGPoint touchLocation = [_tileMap convertTouchToNodeSpace:locationOfTap];
    //convertTouchToNodeSpace requires UITouch

    [_cat moveToward:touchLocation];
}

此处的固定代码 - 这也修复了反向 Y 轴

CGPoint touchLocation = [[CCDirector sharedDirector] convertToGL:[self convertToNodeSpace:[tapRecognizer locationInView:[[CCDirector sharedDirector] openGLView]]]];
4

4 回答 4

94

您可以使用locationInView:UIGestureRecognizer 上的方法。如果为视图传递 nil,此方法将返回窗口中触摸的位置。

- (void)handleTap:(UITapGestureRecognizer *)tapRecognizer
{
    CGPoint touchPoint = [tapRecognizer locationInView: _tileMap]
}

还有一个有用的委托方法gestureRecognizer:shouldReceiveTouch:。只需确保实现并将您的点击手势的委托设置为 self.

保留对手势识别器的引用。

@property UITapGestureRecognizer *theTapRecognizer;

初始化手势识别器

_theTapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget: self action: @selector(someMethod:)];
_theTapRecognizer.delegate = self;
[someView addGestureRecognizer: _theTapRecognizer];

监听委托方法。

-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
    CGPoint touchLocation = [_tileMap convertTouchToNodeSpace: touch];
    // use your CGPoint
    return YES;
}
于 2013-05-17T21:26:23.323 回答
16

在斯威夫特:

func handleFrontTap(gestureRecognizer: UITapGestureRecognizer) {
   print("tap working")
   if gestureRecognizer.state == UIGestureRecognizerState.Recognized
   { 
      print(gestureRecognizer.locationInView(gestureRecognizer.view))
   }
}
于 2016-04-12T04:55:36.703 回答
1

尝试这个:

-(void) didMoveToView:(SKView *)view{
    oneFingerTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(oneTapDetected:)];
    oneFingerTap.numberOfTapsRequired=1;
    oneFingerTap.numberOfTouchesRequired=1;

    [view addGestureRecognizer:oneFingerTap];
}

-(void)oneTapDetected:(UITapGestureRecognizer *)recognizer{
    NSLog(@"one tap detec");
    tapPositionOneFingerTap = [oneFingerTap locationInView:self.view];
    NSLog(@"%f, %f",tapPositionOneFingerTap.x,tapPositionOneFingerTap.y);
}

这将打印控制台中每个点击的坐标。

于 2014-05-15T23:36:47.837 回答
0

苹果文档

UIGestureRecognizer

- (NSUInteger)numberOfTouches

接收方维护的私有数组中 UITouch 对象的数量。

所以你不应该访问它们。

在循环中使用此方法返回的值,您可以使用该方法询问各个触摸的位置locationOfTouch:inView:

于 2015-09-28T23:09:57.920 回答