How to get UITouch location from UIGestureRecognizer

后端 未结 4 762
天命终不由人
天命终不由人 2020-12-08 18:44

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

4条回答
  •  忘掉有多难
    2020-12-08 19:18

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

提交回复
热议问题