General Middleware

Enable Pinch Zoom in your photos in Swift Xcode

  1. Open Xcode and create a new project
  2. Go to Main.storyboard, and add an UIImageView Object and align it to the center of the screen
  3. Select an Image from your Assets and add it to the UIImageView in the Main.storyboard
  4. Add a UIPinchGestureRecognizer object and drop it into the UIImageView created in the previous step
  5. Connect the UIImageView in Main.storyboard to the IBOutlet of the ViewController
  6. Connect the UIPinchGestureRecognizer to a IBOutlet and an IBAction of the ViewController
  7. Below is the Code for the ViewController
//
//  ViewController.swift
//  PhotoZoom
//
//  Created by Animesh Banerjee on 30/4/21.
//

import UIKit

class ViewController: UIViewController, UIScrollViewDelegate {

    @IBOutlet var pinchGesture: UIPinchGestureRecognizer!
    @IBOutlet weak var imageView: UIImageView!
    override func viewDidLoad() {
        super.viewDidLoad()
        imageView.isUserInteractionEnabled = true
    }
    
    @IBAction func addgesture(_ sender: Any) {
        guard let gestureView = pinchGesture.view else {
            return
        }
        gestureView.transform = gestureView.transform.scaledBy(x: pinchGesture.scale, y: pinchGesture.scale)
        pinchGesture.scale = 1.0
    }
    



}

Leave a Reply

Your email address will not be published. Required fields are marked *