Search code examples
iosswiftuiscrollviewuikituigesturerecognizer

swift get position of finger when is pressed longer


I have a scrollview and would like to get the exact position of the finger when the user presses the screen longer. Or if that's possible just get the position of the finger in a scrollview when the finger is moving.

So far I've tried using a LongGestureRecognizer. Here is my code, it all works, but I still need the exact position of the finger. Here how I created the Scrollview and the GestureRecognizer.

    testScroll = UIScrollView(frame: CGRect(x: 0, y: 0, width: width, height: height))
    self.view.addSubview(testScroll)
    testScroll.contentSize = CGSize(width: width, height: height*1.25)
    testScroll.delegate = self
    
    testREc = UILongPressGestureRecognizer(target: self, action: #selector(testprint(sender:)))
    testScroll.addGestureRecognizer(testREc)

if there is a possibility to get the position this way, or exactly via the (testprint func), it would be helpful for me

If you have any tips, I would appreciate your help.


Solution

  • Set the delegate of your UILongPressGestureRecognizer and implement gestureRecognizerShouldBegin().

    There you can get the touch location that triggered the long press.