Identifying Gestures using iOS 16 Gesture Recognizers

In the chapter entitled Detecting iOS 16 Touch Screen Gesture Motions, we looked at how to track the motion of contact with the touch screen of an iOS device. In practice, an app must respond to specific movements during a gesture. Swiping a finger across the screen might, for example, be required to slide a new view onto the display. Similarly, a pinching motion is typically used in iOS apps to enlarge or reduce an image or view.

Before iOS 4, identifying a gesture was the app developer’s responsibility 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 iOS device, Apple introduced the UIGestureRecognizer class in iOS 4, thereby making the task of identifying the types of gestures a much easier task for the app developer.

This chapter aims to provide an overview of gesture recognition within iOS 16. The next chapter will work through An iOS 16 Gesture Recognition Tutorial.

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 detects when a user taps on the device’s screen. Both single and multiple taps may be detected based on the configuration of the class instance.

 

You are reading a sample chapter from Building iOS 17 Apps using Xcode Storyboards.

Buy the full book now in eBook or Print format.

The full book contains 96 chapters and 760 pages of in-depth information.

Learn more.

Preview  Buy eBook  Buy Print

 

  • UIPinchGestureRecognizer – Detects when the user makes a pinching motion 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 the user makes a dragging or panning gesture.
  • UIScreenEdgePanGestureRecognizer – Detects when a dragging or panning gesture starts near the edge of the display screen.
  • 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 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 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 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 the recognizer to be removed as an argument.

Recognizer Action Messages

The iOS gesture recognizers use the target-action model to notify the app 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 if the corresponding gesture is detected.

Discrete and Continuous Gestures

Gestures fall into two distinct categories – discrete and continuous. A discrete gesture only makes a single call to the corresponding action method. Tap gestures (including multiple taps) are considered to be discrete because they only trigger the action method once. On the other hand, 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, the action method may obtain information about a pinch gesture’s scale factor and speed. 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:

 

You are reading a sample chapter from Building iOS 17 Apps using Xcode Storyboards.

Buy the full book now in eBook or Print format.

The full book contains 96 chapters and 760 pages of in-depth information.

Learn more.

Preview  Buy eBook  Buy Print

 

let doubleTap = UITapGestureRecognizer(target: self,
          action: #selector(tapDetected))
doubleTap.numberOfTapsRequired = 2

self.view.addGestureRecognizer(doubleTap)Code language: Swift (swift)

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

@objc func tapDetected() {
	// Code to respond to gesture here
}Code language: Swift (swift)

Recognizing Pinch Gestures

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

let pinchRecognizer = UIPinchGestureRecognizer(target: self,
            action: #selector(pinchDetected))
self.view.addGestureRecognizer(pinchRecognizer)Code language: Swift (swift)

Detecting Rotation Gestures

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

let rotationRecognizer = UIRotationGestureRecognizer(target: self, 
                 action: #selector(rotationDetected))
self.view.addGestureRecognizer(rotationRecognizer)Code language: Swift (swift)

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:

let panRecognizer = UIPanGestureRecognizer(target: self, 
                action: #selector(panDetected))
self.view.addGestureRecognizer(panRecognizer)Code language: Swift (swift)

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

 

You are reading a sample chapter from Building iOS 17 Apps using Xcode Storyboards.

Buy the full book now in eBook or Print format.

The full book contains 96 chapters and 760 pages of in-depth information.

Learn more.

Preview  Buy eBook  Buy Print

 

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:

  • UISwipeGestureRecognizerDirection.right
  • UISwipeGestureRecognizerDirection.left
  • UISwipeGestureRecognizerDirection.up
  • UISwipeGestureRecognizerDirection.down

When programming in Swift, the above constants may be abbreviated to .right, .left, .up, and .down.

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

let swipeRecognizer = UISwipeGestureRecognizer(target: self, 
             action: #selector(swipeDetected))
swipeRecognizer.direction = .up
self.view.addGestureRecognizer(swipeRecognizer)Code language: Swift (swift)

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 class’s minimumPressDuration, numberOfTouchesRequired, numberOfTapsRequired, and allowableMovement properties. For example, 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:

let longPressRecognizer = UILongPressGestureRecognizer(target: self, 
           action: #selector(longPressDetected))
longPressRecognizer.minimumPressDuration = 3
longPressRecognizer.numberOfTouchesRequired = 1
self.view.addGestureRecognizer(longPressRecognizer)Code language: Swift (swift)

Summary

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

 

You are reading a sample chapter from Building iOS 17 Apps using Xcode Storyboards.

Buy the full book now in eBook or Print format.

The full book contains 96 chapters and 760 pages of in-depth information.

Learn more.

Preview  Buy eBook  Buy Print

 


Categories