Difference between revisions of "An Overview of iPhone Multitouch, Taps and Gestures"

From Techotopia
Jump to: navigation, search
Line 33: Line 33:
 
== Touches ==
 
== Touches ==
  
A touch occurs when a finger establishes contact with the screen. When more than one finger touches the screen each fingure registers as a touch up to a maximum of five fingers.  
+
A touch occurs when a finger establishes contact with the screen. When more than one finger touches the screen each figure registers as a touch up to a maximum of five fingers.  
  
 
== Touch Notification Methods ==
 
== Touch Notification Methods ==

Revision as of 20:45, 20 January 2011

In terms of physical points of interaction between the device and the user, the iPhone provides three buttons, a switch and a touch screen. Without question, the user will spend far more time using the touch screen than any other aspect of the device. It is essential, therefore, that any application be able to handle gestures (touches, multitouches, taps, swipes and pinches etc) performed by the user’s fingers on the touch screen.

Before writing code to handle these gestures this chapter will spend some time talking about the responder chain in relation to touch screen events before delving a little deeper into the types of gestures an application is likely to encounter.


Contents


The Responder Chain

In the chapter entitled Understanding iPhone Views, Windows and the View Hierarchy we spent some time talking about the view hierarchy of an appliation’s user interface and how that hierarchy also defined part of the application’s responder chain. In order to fully understand the concepts behind the handling of touch screen gestures it is first necessary to spend a little more time learning about the responder chain.

When the user interacts with the touch screen of an iPhone the hardware detects the physical contact and notifies the operating system. The operating system subsequently creates an event associated with the interaction and passes it into the application’s event queue where it is subsequently picked up the event loop and passed to the current first responder object; the first responder being the object with which the user was interacting when this event was triggered (for example a UIButton or UIView object). If the first responder has been programmed to handle the type of event received it does so (for example a button may have an action defined to call a particular method when it receives a touch event). Having handled the event, the responder then has the option of discarding that event, or passing it up to the next responder in the response chain (defined by the object’s nextResponder property) for further processing, and so on up the chain. If the first responder is not able to handle the event it will also pass it to the next responder in the chain and so on until it either reaches a responder that handles the event or it reaches the end of the chain (the UIApplication object) where it will either be handled or discarded.

Take, for example, a UIView with a UIButton subview. If the user touches the screen over the button then the button, as first responder, will receive the event. If the button is unable to handle the event it will need to be passed up to the view object. If the view is also unable to handle the event it would then be passed to the view controller and so on.

When working with the responder chain, it is important to note that the passing of an event from one responder to the next responder in the chain does not happen automatically. If an event needs to be passed to the next responder, code must be written to make it happen.

Forwarding an Event to the Next Responder

An event may be passed on to the next responder in the response chain by calling the nextResponder method of the current responder, passing through the method that was triggered by the event and the event itself. Take, for example, a situation where the current responder object is unable to handle a touchesBegan event. In order to pass this to the next responder, the touchesBegan method of the current responder will need to make a call as follows:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {  
    [self.nextResponder touchesBegan:touches withEvent:event]; 
}

Gestures

Gesture is an umbrella term used to encapsulate any single interaction between the touch screen and the user between, starting at the point that the screen is touched (by one or more fingers) and the time that the last finger leaves the surface of the screen. Swipes, pinches, stretches and flicks are all forms of gesture.

Taps

A tap, as the name suggests, occurs when the user touches the screen with a single finger and then immediately lifts it from the screen. Taps can be single-taps or multiple-taps and the event will contain information about the number of times a user tapped on the screen.

Touches

A touch occurs when a finger establishes contact with the screen. When more than one finger touches the screen each figure registers as a touch up to a maximum of five fingers.

Touch Notification Methods

<google>ADSDAQBOX_FLOW</google> Touch screen events cause one of four methods on the first responder object to be called. The method that gets called for a specific event will depend on the nature of the interaction. In order to handle events, therefore, it is important to ensure that the appropriate methods from those outlined below are implemented within your responder chain. These methods will be used in the worked example contained in the iPhone Multitouch, Tap and Gesture Event Handling Tutorial chapter of this book:

touchesBegan method

The touchesBegan method is called when the user first touches the screen. Passed to this method are an argument called touches of type NSSet and the corresponding UIEvent object. The touches object contains a UITouch event for each finger in contact with the screen. The tapCount method of any of the UITouch events within the touches set can be called to identify the number of taps, if any, performed by the user. Similarly, the coordinates of an individual touch can be identified from the UITouch event either relative to the entire screen or within the local view itself.

touchesMoved method

The touchesMoved method is called when one or more fingers move across the screen. As fingers move across the screen this method gets called multiple times allowing the application to track the new coordinates and touch count at regular intervals. As with the touchesBegan method, this method is provided with an event object and an NSSet object containing UITouch events for each finger on the screen.

touchesEnded method

This method is called when the user lifts one or more fingers from the screen. As with the previous methods, touchesEnded is provided with the event and NSSet objects.

touchesCancelled method

When a gesture is interrupted due to a high level interrupt, such as the phone detecting an incoming call, the touchesCancelled method is called.

Summary

In order to fully appreciate the mechanisms for handling touch screen events within an iPhone application, it is first important to understand both the responder chain and the methods that are called on a responder depending on the type of interaction. We have covered these basics in this chapter. In the next chapter, entitled An Example iPhone Touch, Multitouch and Tap Application we will use these concepts to create an example application that demonstrates touch screen event handling.