0

I would like to get get the location of first touch when users starts panning from the bottom of the screen and only then read position and use it.

I know that I can check .begin state but don't know how to keep track of the pan if this condition is true. It seemed like very easy thing to do but I didn't manage to do it.

@objc private func handleSwipe(swipe: UIPanGestureRecognizer) {
    if swipe.state == .began && swipe.location(in: self.view).y < self.view.frame.height * 0.15 {
        // do something with swipe.translation(in: self.view).y
    }
}
mikro098
  • 2,173
  • 2
  • 32
  • 48

1 Answers1

0

Just store the coordinates in a variable, and retrieve it when you handle the .changed state. All gesture recogniser callbacks are on the main thread, so you shouldn't have issues with maintaining your own state between these callbacks.

Clafou
  • 15,250
  • 7
  • 58
  • 89
  • But I don't want to start recognizing pan gesture when user has swiped on the bottom half of the screen. When I did something like this when I reached bottom of the view (`self.view.frame.height * 0.15`) then it stopped tracking the touch position. EDIT: Ok, actually I won't need this but I'm curious how it can be achieved – mikro098 Jan 22 '18 at 18:42
  • 1
    Ah I see. You could check initial location in the .began state and cancel the gesture if needed using this approach: https://stackoverflow.com/questions/3937831/how-can-i-tell-a-uigesturerecognizer-to-cancel-an-existing-touch – Clafou Jan 22 '18 at 18:57
  • ...or by also using your own state, maintaining a bool that you use to ignore the gesture in subsequent callbacks until a new .began callback. – Clafou Jan 22 '18 at 19:20
  • ok, I used bools, maybe it's not the cleanest solution but it works – mikro098 Jan 22 '18 at 19:58