iOS Revisited: UIPanGestureRecognizer

Hot

Post Top Ad

Showing posts with label UIPanGestureRecognizer. Show all posts
Showing posts with label UIPanGestureRecognizer. Show all posts

18 Nov 2017

UIRotationGestureRecognizer - Rotate image with Two Fingers in swift

11/18/2017 09:37:00 am 1
In this tutorial we will learn how to rotate an imageView using UIRotationGestureRecognizer.

A rotation gesture is a continuous gesture that occurs when the first two fingers that touch the screen rotate around each other

The iOS UIRotationGestureRecognizer class has a built-in way to detect rotation gesture on any view.

UIRotationGestureRecognizer - Rotate image with Two Fingers in swift

Other Gestures in iOS:

Getting Started:

Firstly create new Xcode project and save it with 'RotationGesture'.

Next open the ViewController.swift file and add the imageView as follow.

Adding ImageView:

First create imageView property as follow:

let imageView = UIImageView()

Next add imageView as sub view and give the auto layout constraints. Add the following code in viewDidLoad() method:

imageView.translatesAutoresizingMaskIntoConstraints = false
imageView.image = UIImage(named: "cat")
imageView.contentMode = .scaleAspectFill
imageView.clipsToBounds = true
view.addSubview(imageView)
imageView.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
imageView.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true
imageView.widthAnchor.constraint(equalTo: view.widthAnchor, multiplier: 0.6).isActive = true
imageView.heightAnchor.constraint(equalTo: imageView.widthAnchor, multiplier: 1).isActive = true

Adding Rotation Gesture:

First create an instance to UIRotationGestureRecognizer() as follow:

var rotationGesture = UIRotationGestureRecognizer()

Next add rotationGesture to the imageView. Add the following code to the end of viewDidLoad() method:

rotationGesture = UIRotationGestureRecognizer(target: self, action: #selector(self.rotationGestureHandler))
imageView.isUserInteractionEnabled = true
imageView.addGestureRecognizer(rotationGesture)

The isUserInteractionEnabled property of the view is set to true. Image views and labels set this property to false by default.

Finally add the action for the rotate gesture, add the following method:

@objc func rotationGestureHandler(recognizer:UIRotationGestureRecognizer){
    if let view = recognizer.view {
        view.transform = view.transform.rotated(by: recognizer.rotation)
        recognizer.rotation = 0
    }
}

Run the project, we will see an image with an rotate gesture as follow:

UIRotationGestureRecognizer - Rotate image with Two Fingers in swift-gif
Download sample project with example :

Read More

16 Nov 2017

UILongPressGestureRecognizer - Image zoom in and out example on Long Press Gesture in swift

11/16/2017 08:54:00 am 0
In this tutorial we will learn how to zoom in and out an imageView using UILongPressGestureRecognizer.

The iOS UILongPressGestureRecognizer class has a built-in way to detect long press gesture on any view.

UILongPressGestureRecognizer - Image zoom in and out example on Long Press Gesture in swift gif

Other Gestures in iOS:


Getting Started:

Firstly create new Xcode project and save it with 'LongPressGesture'.

Next open the ViewController.swift file and add the imageView as follow.

Adding ImageView:

First create imageView property as follow:

let imageView = UIImageView()

Next add imageView as sub view and give the auto layout constraints. Add the following code in viewDidLoad() method:

imageView.translatesAutoresizingMaskIntoConstraints = false
imageView.image = UIImage(named: "cat")
imageView.contentMode = .scaleAspectFill
imageView.clipsToBounds = true
view.addSubview(imageView)
imageView.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
imageView.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true
imageView.widthAnchor.constraint(equalTo: view.widthAnchor, multiplier: 0.4).isActive = true
imageView.heightAnchor.constraint(equalTo: imageView.widthAnchor, multiplier: 1).isActive = true

Adding Long Press Gesture:

First create an instance to UILongPressGestureRecognizer() as follow:

var longPressGesture = UILongPressGestureRecognizer()

Next add longPressGesture to the imageView. Add the following code to the end of viewDidLoad() method:

longPressGesture = UILongPressGestureRecognizer(target: self, action: #selector(self.longPressGestureHandler))
imageView.isUserInteractionEnabled = true
imageView.addGestureRecognizer(longPressGesture)

The isUserInteractionEnabled property of the view is set to true. Image views and labels set this property to false by default.

Finally add the action for the long press gesture, add the following method:

@objc func longPressGestureHandler(recognizer:UIPinchGestureRecognizer){
    switch recognizer.state {
    case .began:
        UIView.animate(withDuration: 0.05,
                       animations: {
                        self.imageView.transform = CGAffineTransform(scaleX: 1.5, y: 1.5)
        },
                       completion: nil)
    case .ended:
        UIView.animate(withDuration: 0.05) {
            self.imageView.transform = CGAffineTransform.identity
        }
    default: break
    }
}

Run the project, we will see an image with an long press gesture as follow:

Tap on image and hold for 1 sec we will see image zoom in as follow:

UILongPressGestureRecognizer - Image zoom in and out example on Long Press Gesture in swift

Download sample project with example :

Read More

15 Nov 2017

UIPanGestureRecognizer - Dragging View Example in Swift

11/15/2017 08:24:00 am 0
In this tutorial we will learn how to drag a view using UIPanGestureRecognizer.

The iOS UIPanGestureRecognizer class has a built-in way to detect pan gesture on any view.

UIPanGestureRecognizer - Dragging View Example in Swift

Other Gestures in iOS:

Single Tap & Double Tap Gesture - UITapGestureRecognizer
Swipe Gesture Recognizer - UISwipeGestureRecognizer
Pinch Gesture Recognizer - UIPinchGestureRecognizer
Long Press Gesture - UILongPressGestureRecognizer
UIRotationGestureRecognizer - Rotate Image View swift

Getting Started:

Firstly create new Xcode project and save it with 'PanGesture'.

Next open the ViewController.swift file and add the imageView as follow.

Adding ImageView:

First create imageView property as follow:

let imageView = UIImageView()

Next add imageView as sub view and give the auto layout constraints. Add the following code in viewDidLoad() method:

imageView.translatesAutoresizingMaskIntoConstraints = false
imageView.image = UIImage(named: "cat")
imageView.contentMode = .scaleAspectFill
imageView.clipsToBounds = true
view.addSubview(imageView)
imageView.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
imageView.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true
imageView.widthAnchor.constraint(equalTo: view.widthAnchor, multiplier: 0.4).isActive = true
imageView.heightAnchor.constraint(equalTo: imageView.widthAnchor, multiplier: 1).isActive = true

Adding Pan Gesture:

First create an instance to UIPanGestureRecognizer() as follow:

var panGesture  = UIPanGestureRecognizer()

Next add panGesture to the imageView. Add the following code to the end of viewDidLoad() method:

panGesture = UIPanGestureRecognizer(target: self, action: #selector(self.panGestureHandler(_:)))
imageView.isUserInteractionEnabled = true
imageView.addGestureRecognizer(panGesture)

The isUserInteractionEnabled property of the view is set to true. Image views and labels set this property to false by default.

Finally add the action for the pan gesture, add the following method:

@objc func panGestureHandler(_ recognizer: UIPanGestureRecognizer){
    self.view.bringSubview(toFront: imageView)
    let translation = recognizer.translation(in: self.view)
    imageView.center = CGPoint(x: imageView.center.x + translation.x, y: imageView.center.y + translation.y)
    recognizer.setTranslation(CGPoint.zero, in: self.view)
}

Run the project, we will see an image with an pan gesture as follow:

UIPanGestureRecognizer - Dragging View Example in Swift

Download sample project with example :

Read More

Post Top Ad