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

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

zoomScale of 1.0 sets the current zoom factor. It is not the minimum or maximum.

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? { // Swift 2; Swift 3 is viewForZooming(in:)
    return imageView
}

Now create UIImageView instance

Make this a property of the class:

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

And then add it to the scroll view. The scroll view itself is never added to the view hierarchy, and contentSize is never set.

scrollView.addSubview(imageView)

Reference

Feedback about page:

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



Table Of Contents