I am building an application where I want to display a floor plan where the image is 1260x1000, larger than the size of my view controller. I want the user to be able to pan the image and zoom in and out, similar to how a map behaves in Mapview. How can I adjust my ScrollView attributes to obtain the desired result?
First, make sure your scroll view has a maximum zoom scale larger than the default of 1.0. You can change this in Interface Builder if you want, or use the maximumZoomScale property in code.
Make your controller delegate of your scroll view .. scrollView.delegate = self
Make sure your view controller conform to the UIScrollViewDelegate protocol, then add the viewForZooming(in:) method, like this:
func viewForZooming(in scrollView: UIScrollView) -> UIView? {
return imageView
}
So basically your code will look similar to this one
class YourViewController: UIViewController, UIScrollViewDelegate {
var scrollView: UIScrollView!
var imageView: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
imageView = UIImageView(image: UIImage(named: "yourImage.png"))
scrollView = UIScrollView(frame: view.bounds)
scrollView.contentSize = imageView.bounds.size
scrollView.addSubview(imageView)
scrollView.delegate = self
scrollView.minimumZoomScale = 0.3
scrollView.maximumZoomScale = 2
view.addSubview(scrollView)
}
func viewForZoomingInScrollView(scrollView: UIScrollView) -> UIView? {
return imageView
}
}