Identifying Gestures using iOS 10 Gesture Recognizers

From Techotopia
Jump to: navigation, search

PreviousTable of ContentsNext
Detecting iOS 10 Touch Screen Gesture MotionsAn iOS 10 Swift Gesture Recognition Tutorial



Writing traditional Swift code? Reactive Swift is the new way to develop iOS apps.

Learn Reactive Swift with RxSwift and write more streamlined asynchronous and concurrent code, implement business rules and behaviors more consistently and better collaborate across platforms.
Click to learn more and save 18% today...


In the chapter entitled Detecting iOS 10 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 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 to slide a new view onto the display. Similarly, a pinching motion is typically used in iOS 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 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 application developer.

The goal of this chapter, therefore, is to provide an overview of gesture recognition within the context of iOS 10. The next chapter will work through An iOS 10 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 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.

UIScreenEdgePanGestureRecognizer – Detects when a dragging or panning gesture is performed starting 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 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 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.


Writing traditional Swift code? Reactive Swift is the new way to develop iOS apps.

Learn Reactive Swift with RxSwift and write more streamlined asynchronous and concurrent code, implement business rules and behaviors more consistently and better collaborate across platforms.
Click to learn more and save 18% today...


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:

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

self.view.addGestureRecognizer(doubleTap) 

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

func tapDetected() {
	// Code to respond to gesture here
}


Writing traditional Swift code? Reactive Swift is the new way to develop iOS apps.

Learn Reactive Swift with RxSwift and write more streamlined asynchronous and concurrent code, implement business rules and behaviors more consistently and better collaborate across platforms.
Click to learn more and save 18% today...

Recognizing Pinch Gestures

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

let pinchRecognizer = UIPinchGestureRecognizer(target: self,
            action: #selector(ViewController.pinchDetected as 
			(ViewController) -> () -> ()))
self.view.addGestureRecognizer(pinchRecognizer) 

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(ViewController.rotationDetected as 
                         ViewController) -> () -> ()))
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:


Writing traditional Swift code? Reactive Swift is the new way to develop iOS apps.

Learn Reactive Swift with RxSwift and write more streamlined asynchronous and concurrent code, implement business rules and behaviors more consistently and better collaborate across platforms.
Click to learn more and save 18% today...

let panRecognizer = UIPanGestureRecognizer(target: self, 
                action: #selector(ViewController.panDetected as 
                         (ViewController) -> () -> ()))
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:

• UISwipeGestureRecognizerDirection.right

• UISwipeGestureRecognizerDirection.left

• UISwipeGestureRecognizerDirection.up

• UISwipeGestureRecognizerDirection.down

Note that 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:


Writing traditional Swift code? Reactive Swift is the new way to develop iOS apps.

Learn Reactive Swift with RxSwift and write more streamlined asynchronous and concurrent code, implement business rules and behaviors more consistently and better collaborate across platforms.
Click to learn more and save 18% today...

let swipeRecognizer = UISwipeGestureRecognizer(target: self, 
             action: #selector(ViewController.swipeDetected as 
		(ViewController) -> () -> ()))
swipeRecognizer.direction = .up
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:

let longPressRecognizer = UILongPressGestureRecognizer(target: self, 
           action: #selector(ViewController.longPressDetected as 
		(ViewController) -> () -> ()))
longPressRecognizer.minimumPressDuration = 3
longPressRecognizer.numberOfTouchesRequired = 1
self.view.addGestureRecognizer(longPressRecognizer) 

Summary

In this chapter we have provided an overview of gesture recognizers and outlined some examples of how to detect the various types of gesture 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.



Writing traditional Swift code? Reactive Swift is the new way to develop iOS apps.

Learn Reactive Swift with RxSwift and write more streamlined asynchronous and concurrent code, implement business rules and behaviors more consistently and better collaborate across platforms.
Click to learn more and save 18% today...



PreviousTable of ContentsNext
An iOS 8 Swift Gesture Recognition Tutorial