UIGestureRecognizer in iOS App Development

Share now

In this blog, we will see how to handle UI Gesture Recognizer in iOS App Development” by Lia Infraservices – the leading iOS App Development Company in Chennai. After reading this article you will be able to understand how to enable custom hand gestures with easy steps.

In simple terms, UI Gesture Recognizer is actually touches and movements of one or more fingers that happen on a specific area of the mobile screen based on the user’s interest. These gestured requires detailed development tools to create easy user interaction with the mobile. Each gesture has unique functionality, so does the iOS programming process.

Basic UIGesture Recognizers in iOS App Development:

Here is the list of built-in gesture recognizers that Apple offers and that you, most likely, use every day in all kinds of android or iOS mobile apps development.

The gesture recognizers that we are going to work with are in the following order:

  1. Tap gesture recognizer
  2. Swipe gesture recognizer
  3. Pan gesture recognizer
  4. Pinch gesture recognizer
  5. Rotation gesture recognizer

1. Tap UIGesture Recognizer in iOS App Development:

It can be used to handle single or multiple taps, either with one or more fingers. Tapping is one of the most usual gestures that users make.

Here’s an example Code:

override func viewDidLoad() 

{

    super.viewDidLoad()

    let tap = UITapGestureRecognizer(target: self, action: #selector(doubleTapped))

    tap.numberOfTapsRequired = 2

    view.addGestureRecognizer(tap)

}

@objc func doubleTapped() 

{

    // do something here

}

2. Swipe UIGesture Recognizer in iOS App Development :

Another important gesture is the swipe, and this class exists just for it. Swiping happens when dragging a finger towards a direction (right, left, top and down). A characteristic example of the swipe gesture exists on the Photos app, where we use our fingers to slide from one photo to another.

Here’s an example Code:

override func viewDidLoad() 

{

 super.viewDidLoad()

 let swipeRight = UISwipeGestureRecognizer(target: self, action: #selector(respondToSwipeGesture))

 swipeRight.direction = .right self.view.addGestureRecognizer(swipeRight) 

let swipeDown = UISwipeGestureRecognizer(target: self, action: #selector(respondToSwipeGesture))

 swipeDown.direction = .down self.view.addGestureRecognizer(swipeDown) 

@objc func respondToSwipeGesture(gesture: UIGestureRecognizer) 

if let swipeGesture = gesture as? UISwipeGestureRecognizer

 {

 switch swipeGesture.direction 

{

 case .right: print(“Swiped right”) 

case .down: print(“Swiped down”) 

case .left: print(“Swiped left”)

 case .up: print(“Swiped up”) default: break 

}

 }

 }

3. Pan UIGesture Recognizer in iOS App Development :

The pan gesture is actually a drag gesture. It’s used when it’s needed to drag views from one point to another.

Here’s an example Code:

override func viewDidLoad() 

{

    super.viewDidLoad()

    let imageView = UIImageView.init(image: UIImage.init(named: “imageToDrag”))

    imageView.sizeToFit()

    imageView.isUserInteractionEnabled = true

    self.view.addSubview(imageView)

    let pan = UIPanGestureRecognizer.init(target: self, action: #selector(handlePan(recognizer:)))

    imageView.addGestureRecognizer(pan)

}

func handlePan(recognizer: UIPanGestureRecognizer) 

{

    let translation = recognizer.translation(in: self.view)

    if let view = recognizer.view 

{

        view.center = CGPoint(x: view.center.x + translation.x, y: view.center.y + translation.y)

    }

    recognizer.setTranslation(CGPoint.zero, in: self.view)

}

4. Pinch UIGesture Recognizer in iOS App Development :

When you view photos on the Photos app and you use your two fingers to zoom in or out to a photo, then you perform a pinch gesture. As you understand, pinching requires two fingers. An object of this class is usually handy to change the transform of a view, and more specifically its scale. 

Here’s an example Code:

override func viewDidLoad() 

{

 super.viewDidLoad() 

self.textview1.userInteractionEnabled = true 

self.textview1.multipleTouchEnabled = true 

self.pinchGesture = UIPinchGestureRecognizer(target: self, action:#selector(pinchRecognized(_:))) self.textview1.addGestureRecognizer(self.pinchGesture) 

// Do any additional setup after loading the view.

 } 

@IBAction func pinchRecognized(_ pinch: UIPinchGestureRecognizer) 

{

 let fontSize = self.textview1.font!.pointSize*(pinch.scale)/2 if fontSize > 12 && fontSize < 32

textview1.font = UIFont(name: self.textview1.font!.fontName, size:fontSize) 

}

 }

5. Rotation UIGesture Recognizer in iOS App Development :

In accordance with the previous gesture, rotation is used to rotate a view using two fingers.

Here’s an example Code:

override func viewDidLoad() 

{

 super.viewDidLoad()

 // Rotate

    let rotateGesture = UIRotationGestureRecognizer(target: self, action: #selector(handleRotate(_:)))

    rotateView.addGestureRecognizer(rotateGesture)

}

// Rotate action

func handleRotate(gesture: UIRotationGestureRecognizer) 

{

    label.text = “Rotate recognized”

   if gesture.state == UIGestureRecognizerState.Changed 

{

        let transform = CGAffineTransformMakeRotation(gesture.rotation)

        rotateView.transform = transform

    }

Other UIGesture Recognizers in iOS App Development:

  1. LongPress GestureRecognizer
  2. ScreenEdgePanGestureRecognizer

(They are used more rarely and have similarities to gesture recognizers.)

1. LongPress Gesture Recognizer :

An object of that class monitors for long-press gestures happening on a view. The pressing must last long enough in order to be detected, and the finger or fingers should not move a lot around the pressed point otherwise the gesture fails.

2. ScreenEdgePan Gesture Recognizer :

This one is similar to the swipe gesture, but with a great difference: The finger movement should always begin from an edge of the screen.

Conclusion:

Thanks for reading our blog on how to handle UI Gesture Recognizer in iOS App Development. As you know working with gesture recognizers is easy enough, and it consists of a pretty awesome way to provide interaction to your app without using the traditional views for performing actions such as buttons. Gesture Recognizer can be added in 2 ways i,e. either by adding a User interface or through coding programmatically.

Whichever way you would like to choose, we at Lia Infraservices – The Best Mobile App Development Company in Chennai can help you develop your gesture recognizer that suits your specific iOS mobile app development needs. In each of the guest recognizer development, we will write test cases and develop programs that ensure the custom finger gestures work properly.

Our range of mobile app development packages also included iOS & Android App Development Company in Chennai. Custom eCommerce website development company in Chennai, Digital Marketing Company in Chennai.

If you find this blog useful, you may also like reading this blog on Mobile Single Screen Layout with a video view and multiple gestures.

Was this article helpful?

Leave a Reply

Your email address will not be published.