How to get UITouch location from UIGestureRecognizer

IosObjective CXcodeCocos2d IphoneUigesturerecognizer

Ios Problem Overview


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?

- (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

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

Ios Solutions


Solution 1 - Ios

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.

- (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.

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

Solution 2 - Ios

In Swift:

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

Solution 3 - Ios

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.

Solution 4 - Ios

Apple Docs say

> UIGestureRecognizer > > - (NSUInteger)numberOfTouches > > The number of UITouch objects in a private array maintained by the receiver.

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionOscar ApelandView Question on Stackoverflow
Solution 1 - IosMJNView Answer on Stackoverflow
Solution 2 - Iosuser3108511View Answer on Stackoverflow
Solution 3 - Iosuser3352358View Answer on Stackoverflow
Solution 4 - Iosuser1951992View Answer on Stackoverflow