Zoom InOut UIImageView

suggest change

Create UIScrollView instance

let scrollview = UIScrollView.init(frame: self.view.bounds)

And then set these properties:

scrollView.minimumZoomScale = 0.1
scrollView.maximumZoomScale = 4.0
scrollView.zoomScale = 1.0
scrollview.delegate = self as? UIScrollViewDelegate

To zoom in and out image we must specify the amount the user can zoom in and out. We do this by setting values of the scroll view’s minimumZoomScale and maximumZoomScale properties. Both of these are set to 1.0 by default.

And zoomScale to 1.0 which specify the zoom factor for the minimum and maximum zooming.

To support zooming, we must set a delegate for your scroll view. The delegate object must conform to the UIScrollViewDelegate protocol. That delegate class must implement the viewForZoomingInScrollView() method and return the view to zoom.

Modify your ViewController as shown

class ViewController: UIViewController, UIScrollViewDelegate

Then add the following delegate function to the class.

func viewForZoomingInScrollView(scrollView: UIScrollView) -> UIView? {
    return imageView
}

Now create UIImageView instance

Make this variable as class variable

var imageView:UIImageView = UIImageView.init(image: UIImage.init(named: "someImage.jpg"))

And then add it to scrollview

scrollView?.addSubview(imageView)

Reference

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents