Difference between revisions of "Identifying iPhone Gestures using iOS 5 Gesture Recognizers"

From Techotopia
Jump to: navigation, search
m (Text replacement - "<google>BUY_IOS5</google>" to "<htmlet>ios9_upgrade</htmlet>")
m (Text replacement - "<table border="0" cellspacing="0" width="100%">" to "<table border="0" cellspacing="0">")
Line 1: Line 1:
<table border="0" cellspacing="0" width="100%">
+
<table border="0" cellspacing="0">
 
<tr>
 
<tr>
 
<td width="20%">[[Detecting iOS 5 iPhone Touch Screen Gesture Motions|Previous]]<td align="center">[[iPhone iOS 5 Development Essentials|Table of Contents]]<td width="20%" align="right">[[An iPhone iOS 5 Gesture Recognition Tutorial|Next]]</td>
 
<td width="20%">[[Detecting iOS 5 iPhone Touch Screen Gesture Motions|Previous]]<td align="center">[[iPhone iOS 5 Development Essentials|Table of Contents]]<td width="20%" align="right">[[An iPhone iOS 5 Gesture Recognition Tutorial|Next]]</td>
Line 145: Line 145:
  
 
<hr>
 
<hr>
<table border="0" cellspacing="0" width="100%">
+
<table border="0" cellspacing="0">
 
<tr>
 
<tr>
 
<td width="20%">[[Detecting iOS 5 iPhone Touch Screen Gesture Motions|Previous]]<td align="center">[[iPhone iOS 5 Development Essentials|Table of Contents]]<td width="20%" align="right">[[An iPhone iOS 5 Gesture Recognition Tutorial|Next]]</td>
 
<td width="20%">[[Detecting iOS 5 iPhone Touch Screen Gesture Motions|Previous]]<td align="center">[[iPhone iOS 5 Development Essentials|Table of Contents]]<td width="20%" align="right">[[An iPhone iOS 5 Gesture Recognition Tutorial|Next]]</td>

Revision as of 14:30, 5 May 2016

PreviousTable of ContentsNext
Detecting iOS 5 iPhone Touch Screen Gesture MotionsAn iPhone iOS 5 Gesture Recognition Tutorial


Learn SwiftUI and take your iOS Development to the Next Level
SwiftUI Essentials – iOS 16 Edition book is now available in Print ($39.99) and eBook ($29.99) editions. Learn more...

Buy Print Preview Book


In the chapter entitled Detecting iOS 5 iPhone Touch Screen Gesture Motions we looked at how to track the motion of contact with the iPhone screen. In practice, an application will need to respond to specific motions that take place during the course of a gesture. The swiping of a finger across the screen might, for example, be required slide a new view onto the display. Similarly, a pinching motion is typically used in iPhone applications to enlarge or reduce an image or view.

Prior to iOS 4, the identification of a gesture was the responsibility of the application developer and typically involved the creation of complex mathematical algorithms. In recognition of this complexity and given the importance of gestures to user interaction with the iPhone Apple introduced the UIGestureRecognizer class in iOS 4 thereby making the task of identifying the types of gestures a much easier task for the application developer.

The goal of this chapter, therefore, is to provide an overview of gesture recognition within the context of iOS 5 and the iPhone. The next chapter will work through An iPhone iOS 5 Gesture Recognition Tutorial.


Contents


The UIGestureRecognizer Class

The UIGestureRecognizer class is used as the basis for a collection of subclasses, each designed to detect a specific type of gesture. These subclasses are as follows:

  • UITapGestureRecognizer – This class is designed to detect when a user taps on the screen of the device. Both single and multiple taps may be detected based on the configuration of the class instance.
  • UIPinchGestureRecognizer – Detects when a pinching motion is made by the user on the screen. This motion is typically used to zoom in or out of a view or to change the size of a visual component.
  • UIPanGestureRecognizer – Detects when a dragging or panning gesture is made by the user.
  • UISwipeGestureRecognizer – Used to detect when the user makes a swiping gesture across the screen. Instances of this class may be configured to detect motion only in specific directions (left, right, up or down).
  • UIRotationGestureRecognizer – Identifies when the user makes a rotation gesture (essentially two fingers in contact with the screen located opposite each other and moving in a circular motion).
  • UILongPressGestureRecognizer – Used to identify when the user touches the screen with one or more fingers for a specified period of time (also referred to as “touch and hold”).

These gesture recognizers must be attached to the view on which the gesture will be performed via a call to the view object’s addGestureRecognizer: method. Recognizers must also be assigned an action method that is to be called when the specified gesture is detected. Gesture recognizers may subsequently be removed from a view via a call to the view’s removeGestureRecognizer: method, passing through as an argument the recognizer to be removed.

Recognizer Action Messages

The iOS 5 gesture recognizers use the target-action model to notify the application of the detection of a specific gesture. When an instance of a gesture recognizer is created it is provided with the reference to the method to be called in the event that the corresponding gesture is detected.


Discrete and Continuous Gestures

Gestures fall into two distinct categories – discrete and continuous. A discrete gesture results in only a single call being made to the corresponding action method. Tap gestures (including multiple taps) are considered to be discrete because they only trigger the action method once. Gestures such as swipes, pans, rotations and pinches are deemed to be continuous in that they trigger a constant stream of calls to the corresponding action methods until the gesture ends.

Obtaining Data from a Gesture

Each gesture action method is passed as an argument a UIGestureRecognizer sender object which may be used to extract information about the gesture. For example, information about the scale factor and speed of a pinch gesture may be obtained by the action method. Similarly, the action method assigned to a rotation gesture recognizer may ascertain the amount of rotation performed by the user and the corresponding velocity.

Recognizing Tap Gestures

Tap gestures are detected using the UITapGestureRecognizer class. This must be allocated and initialized with an action selector referencing the method to be called when the gesture is detected. The number of taps that must be performed to constitute the full gesture may be defined by setting the numberOfTapsRequired property of the recognizer instance. The following code, for example, will result in a call to the tapsDetected method when two consecutive taps are detected on the corresponding view:

UITapGestureRecognizer *doubleTap = 
          [[UITapGestureRecognizer alloc]
          initWithTarget:self 
          action:@selector(tapDetected:)];
doubleTap.numberOfTapsRequired = 2;
[self.view addGestureRecognizer:doubleTap];

A template method for the action method for this and other gesture recognizers is as follows:

- (IBAction)tapDetected:(UIGestureRecognizer *)sender {
	// Code to respond to gesture here
}

Recognizing Pinch Gestures

Pinch gestures are detected using the UIPinchGestureRecognizer class. For example:

UIPinchGestureRecognizer *pinchRecognizer = 
      [[UIPinchGestureRecognizer alloc]
      initWithTarget:self 
      action:@selector(pinchDetected:)];
[self.view addGestureRecognizer:pinchRecognizer];

Detecting Rotation Gestures

Rotation gestures are recognized by the UIRotationGestureRecognizer, the sample code for which is as follows:

UIRotationGestureRecognizer *rotationRecognizer = 
     [[UIRotationGestureRecognizer alloc]
     initWithTarget:self 
     action:@selector(rotationDetected:)];
[self.view addGestureRecognizer:rotationRecognizer];

Recognizing Pan and Dragging Gestures

Pan and dragging gestures are detected using the UIPanGestureRecognizer class. Pan gestures are essentially any continuous gesture. For example, the random meandering of a finger across the screen will generally be considered by the recognizer as a pan or drag operation:

UIRotationGestureRecognizer *panRecognizer = 
     [[UIPanGestureRecognizer alloc]
     initWithTarget:self 
     action:@selector(panDetected:)];
[self.view addGestureRecognizer:panRecognizer];

If both swipe and pan recognizers are attached to the same view it is likely that most swipes will be recognized as pans. Caution should be taken, therefore, when mixing these two gesture recognizers on the same view.

Recognizing Swipe Gestures

Swipe gestures are detected using the UISwipeGestureRecognizer class. All swipes, or just those in a specific direction, may be detected by assigning one of the following constants to the direction property of the class:

  • UISwipeGestureRecognizerDirectionRight
  • UISwipeGestureRecognizerDirectionLeft
  • UISwipeGestureRecognizerDirectionUp
  • USwipeIGestureRecognizerDirectionDown

If no direction is specified the default is to detect rightward swipes. The following code configures a UISwipeGestureRecognizer instance to detect upward swipes:

UISwipeGestureRecognizer *swipeRecognizer = 
      [[UISwipeGestureRecognizer alloc]
      initWithTarget:self 
      action:@selector(swipeDetected:)];
swipeRecognizer.direction = UISwipeGestureRecognizerDirectionUp;
[self.view addGestureRecognizer:swipeRecognizer];;

Recognizing Long Touch (Touch and Hold) Gestures

Long touches are detected using the UILongPressGestureRecognizer class. The requirements for the gesture may be specified in terms of touch duration, number of touches, number of taps and allowable movement during the touch. These requirements are specified by the minimumPressDuration, numberOfTouchesRequired, numberOfTapsRequired and allowableMovement properties of the class respectively. The following code fragment configures the recognizer to detect long presses of 3 seconds or more involving one finger. The default allowable movement is not set and therefore defaults to 10 pixels:

UILongPressGestureRecognizer *longPressRecognizer = 
     [[UILongPressGestureRecognizer alloc]
     initWithTarget:self 
     action:@selector(longPressDetected:)];
longPressRecognizer.minimumPressDuration = 3;
longPressRecognizer.numberOfTouchesRequired = 1;
[self.view addGestureRecognizer:longPressRecognizer];

Summary

In this chapter we have provided an overview of gesture recognizers and provided some examples of how to detect the various types of gesture typically used by iPhone users. In the next chapter we will work step-by-step through a tutorial designed to show these theories in practice.


Learn SwiftUI and take your iOS Development to the Next Level
SwiftUI Essentials – iOS 16 Edition book is now available in Print ($39.99) and eBook ($29.99) editions. Learn more...

Buy Print Preview Book



PreviousTable of ContentsNext
Detecting iOS 5 iPhone Touch Screen Gesture MotionsAn iPhone iOS 5 Gesture Recognition Tutorial