ios 如何从 UIGestureRecognizer 获取 UITouch 位置

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16618109/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 23:37:41  来源:igfitidea点击:

How to get UITouch location from UIGestureRecognizer

iosobjective-cxcodecocos2d-iphoneuigesturerecognizer

提问by Oscar Apeland

I want to get the UITouch location of my tap from UIGestureRecognizer, but I can not figure out how to from looking at both the documentation and other SO questions. Can one of you guide me?

我想从 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];
}

FIXED CODE HERE - THIS ALSO FIXES INVERTED Y AXIS

固定代码在这里 - 这也固定倒 Y 轴

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

回答by MJN

You can use the locationInView:method on UIGestureRecognizer. If you pass nil for the view, this method will return the location of the touch in the window.

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

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

There is also a helpful delegate method gestureRecognizer:shouldReceiveTouch:. Just make sure to implement and set your tap gesture's delegate to self.

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

Keep a reference to the gesture recognizer.

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

@property UITapGestureRecognizer *theTapRecognizer;

Initiailze the gesture recognizer

初始化手势识别器

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

Listen for delegate methods.

侦听委托方法。

-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
    CGPoint touchLocation = [_tileMap convertTouchToNodeSpace: touch];
    // use your CGPoint
    return YES;
}

回答by user3108511

In Swift:

在斯威夫特:

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

回答by user3352358

Try this:

尝试这个:

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

This prints the coordinates of each tap in your console.

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

回答by user3352358

Apple Docssay

苹果文档

UIGestureRecognizer

- (NSUInteger)numberOfTouches

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

UIGestureRecognizer

- (NSUInteger)numberOfTouches

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

So you shouldn't access them.

所以你不应该访问它们。

Using the value returned by this method in a loop, you can ask for the location of individual touches using the locationOfTouch:inView:method.

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