如何从UIGestureRecognizer获取UITouch位置

57

我想从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个回答

96
你可以在UIGestureRecognizer上使用locationInView:方法。如果你传递了 nil 作为视图,这个方法将返回触摸在窗口中的位置。
- (void)handleTap:(UITapGestureRecognizer *)tapRecognizer
{
    CGPoint touchPoint = [tapRecognizer locationInView: _tileMap]
}

还有一个有用的委托方法gestureRecognizer:shouldReceiveTouch:。只需确保实现并将您的轻拍手势的委托设置为自己。

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

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

我需要一个UITouch对象,用于我的CGPoint touchLocation = [_tileMap convertTouchToNodeSpace:locationOfTap];代码中。其中convertTouchToNodeSpace方法将一个UITouch对象转换为一个CGPoint对象。这段代码可以这样使用吗? - Oscar Apeland
我找不到它的定义在哪里,哈哈哈 - Oscar Apeland
不,它是cocos2d,开源的。 - Oscar Apeland
  • (CGPoint)convertTouchToNodeSpace:(UITouch *)touch { CGPoint point = [touch locationInView: [touch view]]; point = [[CCDirector sharedDirector] convertToGL: point]; return [self convertToNodeSpace:point]; }
当我将其更改为CGPoint时,它并不会感到高兴。
- Oscar Apeland
看起来我实际上可以直接通过CGPoint location = [tapRecognizer locationInView:[[CCDirector sharedDirector] openGLView]];将一个CGpoint传递给moveToward函数,而问题可能出在其他地方.. :s 无论如何,还是会接受你的答案,因为它对我有帮助。 - Oscar Apeland
显示剩余4条评论

17

在Swift中:

func handleFrontTap(gestureRecognizer: UITapGestureRecognizer) {
   print("tap working")
   if gestureRecognizer.state == UIGestureRecognizerState.Recognized
   { 
      print(gestureRecognizer.locationInView(gestureRecognizer.view))
   }
}

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

这会在您的控制台中打印每次点击的坐标。

0

苹果文档 表示

UIGestureRecognizer

- (NSUInteger)numberOfTouches

The number of UITouch objects in a private array maintained by the receiver.

所以你不应该访问它们。

使用此方法返回的值,在循环中,您可以使用 locationOfTouch:inView: 方法请求单个触摸的位置。


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