0

Share

APP DEVELOPMENT

How To Use Gestures In iOS Swift?

  • Home
  • Blogs
  • How To Use Gestures In iOS Swift?

A gesture recognizer identifies a sequence of touches (or other input) and acts on that recognition. It comprises actions such as select, scroll, and much more.

Somish Kakadiya

July 13, 2022

Most people around the world are using smartphones that work with our hand gestures like scrolling, moving, tapping, zooming, swiping, and many more actions. 

Gestures are the touches and movements of one or more fingers on a device screen. And a gesture recognizer is an object of the abstract class UI Gesture Recognizer. This object is related to the view which monitors predefined gestures.

There are various types of gestures that are required to be implemented in your application development. Take a look.

Type Of Gestures

  • Tap Gesture
  • LongPressed Gesture
  • Rotation Gesture
  • Pinch Gesture
  • Pan Gesture
  • Swipe Gesture

Before starting about gestures let’s create one new project for it. Follow steps to create a new project as below.

Step-1:

Open Xcode and create new single view app.




Step-2:

Add UIView in storyboard, apply background color to identify it and make outlet of that view.




Now, we’ll apply different gesture on this view.

Note: Here we’ll apply gesture on view only. Same method we can use for applying gesture on UILabel, UIImageView, and other components also.

Tap Gesture




Tap gesture is used to detect tap (or we can say the number of taps) on view. For add “Tap Gesture” on view we need to initialize UITapGestureRecognizer with a target and action.

In “viewDidLoad” write code:

In the above code, first of all there is initialization for “Tap Gesture” with handler method. Then user can define “numberOfTapsRequired”. Using it we can set how many taps are required for getting event of tap gesture.

And in last add this gesture on required view.

For handle above tap gesture we need to make method which will fire when user will tap on view.

LongPressed Gesture




“LongPressed Gesture” is used when user wants to identify long press on view.

For set long press gesture, In viewDidLoad write below code.

In above code we’ve initialize “UILongPressGestureRecognizer” with target method.

Then after give optional value “minimumPressDuration” to define minimum time user have to press view for get long press event. In last applied that gesture on required view.

For handle long press event define method as below.

This will fire when user pressed view for minimum 0.5 second. We can vary this time by changing the value of “minimumPressDuration” while initialize long pressed gesture.

Rotation Gesture




This gesture is used when user wants to rotate view.

Initialize “Rotation gesture” as below and apply on desired view:

Here code will apply transform on view as per rotation. So view will rotate.

Pinch Gesture




Pinch gesture is used for zoom-in or zoom-out of view using two fingers.

As like above gesture to make view zoomable, we need to initialize “Pinch gesture” and apply it on view.

For add pinch gesture on view, add code in viewDidLoad:

When user will zoom-in / zoom-out view above code handle it. As per scaling provided by user’s finger above code will transform view.

Pan Gesture




Pan gesture is used to move view as per user’s interaction.

Initialize pan gesture as below.

In the above code, first of all we had initialized pan gesture and then applied it on desired view.

For handle event of this pan gesture define method as below.

Here in the above code, at the very first we detect where the user's finger is moving. After getting the location of user’s finger in view, we translate it. As per translation we need to set desired view’s center. So, When user moves finger our desired view’s center will follow it.

Swipe Gesture




If user wants to move view in particular horizontal or vertical direction, “Swipe Gesture” is used at that time.

In swipe gesture we can define the direction in which we want to move view.

Let’s see how we can initialize swipe gesture.

Here I’ve initialize two swipe gestures, one with swipe direction “right” and second is with direction “up”. We can give direction in which we want to move our desired view. In addition of “right” and “up” direction we can also give “left” and “down” directions.

After initialize and define gestures apply on desired view.

Instead of making separate swipe gestures object for different direction we can also set more then one direction in single swipe gestures. Here to understand more clearly I’d make it separate. But handler for both event is single. We can detect direction in single handler method as below.

We can perform different actions on different swipes of view.

It is a basic idea about different types of gestures used in iOS. We can add action or can perform desired action when particular gesture is detected.

Conclusion

This blog is filled with various types of gestures that are used in the development of the Swift application. It will help you to create the best-featured applications for your clients.

img

Somish Kakadiya

CTO of Vasundhara Infotech, a leading Software development company in the USA. His technological interests has helped the company in making useful decisions.

message

Have a project in mind? Drop a message to Bansi Pipaliya & start the discussion!

Get a Newsletter

Sign Up to our newsletter to get latest updates staight in your inbox.

Vasundhara respects your privancy. No Spam!

Get a Newsletter

Sign Up to our newsletter to get latest updates staight in your inbox.

Vasundhara respects your privancy. No Spam!

message

Have a project in mind? Drop a message to Bansi Pipaliya & start the discussion!

Latest 13 Web Development Trends To Expect In 2022
April 11, 2022 Category : company news

Revealing Vasundhara’s New Identity

Read More
Latest 13 Web Development Trends To Expect In 2022
May 17, 2022 Category : web development

Latest 13 Web Development Trends To Expect In 2022

Read More
Leave a Comment