Make Your App More Interactional By Implementing iOS Gesture Recognizer

0
Shares
Make Your App More Interactional By Implementing iOS Gesture Recognizer
Rate this post

If you want to detect different gestures like pan gesture, pin gesture, or rotation gestures in your iOS app, it has now become more easy with iOS Gesture Recogizer.

The gesture recognizer is nothing but an object of the abstract class UIGestureRecognizer. And this object is usually related to view, which monitor the gestures that are predefined.

In this iOS app tutorial, we’ll show you how to add different gestures into your app. And to demonstrate, we’ll build a simple demo in which we’ll cover how to add those different gestures in your native iOS app.

Let’s Get Started

Create a new project with “Single View Application” and press on the next button.

Gesture1

Enter the name of project in “Project Name” field and select swift in “Language” field.

Gesture2

Create a new swift file with name “GestureVC.swift” and add UIViewController in Main.storyboard.

Gesture3

 

Gesture4

 

Add a view in Main.storyboard -> GestureVC and set outlet of view.

@IBOutlet weak var viewGesture: UIView!

Set UITapGestureRecognizer  to “viewGesture”  in “ viewDidLoad()”

//For Tap Gesture Recoginzation

            let TapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(self.handleTapGesture))

            self.viewGesture.addGestureRecognizer(TapGestureRecognizer)

Now handle tap gesture using following method.

//MARK: Handle Tap Gesture

    func handleTapGesture(tapGestureRecognizer: UITapGestureRecognizer) {

        if CGColorEqualToColor(viewGesture.backgroundColor!.CGColor, UIColor.greenColor().CGColor) {

            viewGesture.backgroundColor = UIColor.blueColor()

        } else {

            viewGesture.backgroundColor = UIColor.greenColor()

        }

    }

tap on viewGesture will change background colors.

you can perform other functions like animation too instead.

If you want to apply Swipe gesture you need add UISwipeGestureRecognizer in “viewDidLoad()”

//For Swipe Gesture Recoginzation

            let directions: [UISwipeGestureRecognizerDirection] = [.Right, .Left, .Up, .Down]

            for direction in directions {

                let SwiptGestureRecognizer = UISwipeGestureRecognizer(target: self, action: #selector(GestureVC.handleSwipeGesture(_:)))

                SwiptGestureRecognizer.direction = direction

                viewGesture.addGestureRecognizer(SwiptGestureRecognizer)

            }

to handle swipe gesture, you can use following method which we have added as selector in SwiptGestureRecognizer object.

//MARK: Handle Swipe Gesture

    func handleSwipeGesture(sender: UISwipeGestureRecognizer) {

        print(sender.direction)

    }

it will provide you the direction of your finger swipe on screen.

and you can perform any kind of animation.

If you want to apply Swipe gesture you need add UIPanGestureRecognizer in “viewDidLoad()”

//For Pan Gesture Recoginzation

            let PanGestureRecognizer = UIPanGestureRecognizer(target: self, action: #selector(handlePanGesture))

            viewGesture.addGestureRecognizer(PanGestureRecognizer)

To handle swipe gesture you can use following method which we have added as selector in PanGestureRecognizer object.

//MARK: Handle Pan Gesture

    func handlePanGesture(gestureRecognizer: UIPanGestureRecognizer) {

        if gestureRecognizer.state == .Began || gestureRecognizer.state == .Changed {

            

            let translation = gestureRecognizer.translationInView(self.view)

            // note: 'view' is optional and need to be unwrapped

            gestureRecognizer.view!.center = CGPointMake(gestureRecognizer.view!.center.x + translation.x, gestureRecognizer.view!.center.y + translation.y)

            gestureRecognizer.setTranslation(CGPointMake(0,0), inView: self.view)

        }  

    }

If you want to apply Swipe gesture you need add UIPinchGestureRecognizer in “viewDidLoad()”

//For Pinch Gesture Recoginzation

            let PinchGestureRecognizer: UIPinchGestureRecognizer = UIPinchGestureRecognizer(target: self, action: #selector(GestureVC.handlePinchGesture(_:)))

            viewGesture.addGestureRecognizer(PinchGestureRecognizer)

To handle swipe gesture, you can use following method which we have added as selector in PinchGestureRecognizer object.

//MARK: Handle Pinch Gesture

    func handlePinchGesture(sender: UIPinchGestureRecognizer) {

        sender.view!.transform = CGAffineTransformScale(sender.view!.transform,

                                                        sender.scale, sender.scale)

        sender.scale = 1

    }

If you want to apply Swipe gesture you need add UIRotationGestureRecognizer in “viewDidLoad()”

//For Rotate Gesture Recoginzation

            let RotateGestureRecognizer: UIRotationGestureRecognizer = UIRotationGestureRecognizer(target: self, action: #selector(GestureVC.handleRotateGesture(_:)))

            viewGesture.addGestureRecognizer(RotateGestureRecognizer)

To handle swipe gesture, you can use following method which we have added as selector in RotateGestureRecognizer object.

//MARK: Handle Rotate Gesture

    func handleRotateGesture(sender: UIRotationGestureRecognizer) {

        sender.view!.transform = CGAffineTransformRotate(sender.view!.transform, sender.rotation)

        sender.rotation = 0

    }

 

This is how it will look like:

And Done!

Easy, right?

Or if you faced any problem, you can simply contact our developers for help.

Also, when there is lack of experience and/or technical knowledge, it’s recommended to hire iOS app developer, rather than doing it yourself. Because let’s admit it, either it could get complicated or completely unsuccessful.

And if you’re new to all this, and feeling uncomfortable with implementing gesture recognizers, you can always contact us to do the job, the right way!

Grab a free copy of iOS Gesture Recognizer demo from Github.

This page was last edited on November 18th, 2016, at 16:40.
 
0
Shares
 

Have an App Idea?

Get your free consultation now