UITouch, one of the most basic objects a dev will come across when dealing with touches in iOS.
Phase property on UITouch:
A finger has touched the screen
A finger moved on the screen. This can be helpful to know, because if a user is moving their finger across the screen longer than a simple touch, then we know that it is not just a tap but a gesture.
A finger is stationary. The user is completing a long press with the expectation that something is happening. This is typically done in a messaging app where we want to highlight some text or copy and paste something.
A finger was lifted off of the screen
If the touch is interrupted by the system, such as an alert view appears, or the user receives a phone call.
Although these are pretty self explanatory, they are important because they tell you the state of the touch, and will consistently come up when using gesture recognizers. You need to know which state the app is in so we can return it to where it needs to be or where it needs to go on completion of the touch.
How touch events normally work:
- Hit Testing finds Hit-Test view
Let’s say the user touches view B on the phone. The touch recognizer finds that view B is the hit test view, and
- Touches begin with event.
Touches begin when the event is called.
- Touches moved with event.
This happens as the user moves his or her finger. Continues to be called as long as the user is moving his or her finger.
- Touches ended with event.
Touch is ended when the user lifts his or her finger.
Let’s see what happens when the gesture recognizer is attached to that same view.
- Hit testing finds hit-test view. Gesture state “Possible”
When the user touches the same view B, hit testing still runs. And our gesture recognizer is sitting in the Possible state.
- Touches began with event. Gesture state “Possible”
Gesture Recognizer acknowledges the event, and is still sitting in the possible state.
- Gesture recognized – state “Began”
Now, the user moves their finger very slightly. This is where gesture recognizer differs from touch event. The gesture recognizer is actually going to enter the Began state. It is basically saying that it recognizes the user completing a particular gesture, which in this case is a pan gesture.
- Touches Cancelled when Event is called.
Touch event is now cancelled on the original touch event, and UIGestureRecognizer is going to take over the rest of the touch event cycle. So the user continues to move their finger..
- Gesture recognized – state “changed”.
As the user continues to move their finger, UIGestureRecognizer enters the “Changed” state. Instead of Touches Moved with event being called, which would the normal state to happen.
- Gesture recognized – state “ended”.
When the user completes their action and lift their finger, gesture recognizer enters the ended state.