Search code examples
iosviewswift3zoominguipinchgesturerecognizer

How do i pinch to zoom the entire screen (including labels, buttons and images)? IOS, Swift 3


This code doesn't seem to work. wondering what I'm doing wrong/missing. I assume that I: drag and drop the UIPinchGestureRecognizer to the viewcontroller (so that it doesn't interfere with the connections to buttons, label etc. and it is connected to the viewcontroller); then implement this code (make sure connections are proper):

import UIKit

class AlertController: UIAlertController {

@IBAction func scaleImage(sender: UIPinchGestureRecognizer) {
self.view.transform = CGAffineTransformScale(self.view.transform,     sender.scale, sender.scale)
sender.scale = 1
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
view.backgroundColor = UIColor.blackColor()
}

override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning()
}

}

Please help !


Solution

  • You can put your zoomeable UIView inside an UIScrollView an then implement this method

    func viewForZooming(in scrollView: UIScrollView) -> UIView? {
        return self.view
    }
    

    you need also setup your scrollView delegate and set your self.scrollView.maximumZoomScale > self.scrollView.minimumZoomScale something like this

        self.scrollView.delegate = self
        self.scrollView.maximumZoomScale = 2.0
        self.scrollView.minimumZoomScale = 0.5
    

    then using pitch gesture must scale your `self.view

    I hope this helps you, best regards