Adventures in [UITouch]

UITouch, one of the most basic objects a dev will come across when dealing with touches in iOS.

Phase property on UITouch:

ULTouchPhaseBegan

A finger has touched the screen

UITouchPhaseMoved

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.

UITouchPhaseStationary

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.

UITouchPhaseEnded

A finger was lifted off of the screen

UITouchPhaseCancelled

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:

Touch Event:

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

  1. Touches begin with event.

Touches begin when the event is called.

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

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

Gesture Recognizer

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

  1. Touches began with event. Gesture state “Possible”

Gesture Recognizer acknowledges the event, and is still sitting in the possible state.

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

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

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

  1. Gesture recognized – state “ended”.

When the user completes their action and lift their finger, gesture recognizer enters the ended state.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s