I was hoping someone could help me out. I am trying to allow a user to pinch zoom on a UIImageView(with a max and min level allowed). But for some reason the it does not wor
I think the biggest problem is at the end of your func
, you have sender.scale = 1
. If you remove that line of code, your image shouldn't just bounce back each time.
Swift 3 solution
This is the code I used. I added imageView to scrollView as a subview.
class ZoomViewController: UIViewController,UIScrollViewDelegate {
@IBOutlet weak var scrollView:UIScrollView!
@IBOutlet weak var imageView:UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
scrollView.delegate = self
scrollView.minimumZoomScale = 1.0
scrollView.maximumZoomScale = 10.0//maximum zoom scale you want
scrollView.zoomScale = 1.0
}
func viewForZooming(in scrollView: UIScrollView) -> UIView? {
return imageView
}
This is an old question but I don't see any answers that explain what is wrong with the original code.
This line:
let currentScale = self.view.frame.size.width / self.view.bounds.size.width
Is working on the main view rather than the imageView so the scale calculation is always ~1
This simple change makes it behave as expected
let currentScale = sender.view!.frame.size.width / sender.view!.bounds.size.width
by changing self to sender (and forcing view to unwrap) the scale calculation works as expected.
I posted an answer here for zoom functionality with both pinch and double tap in Swift 3. Works perfectly.
I decided to add the imageView to a UIScrollView. It allows the user to zoom and pan over. Here is the code I used.
in order to set max/min zoom I used :
scrollImg.minimumZoomScale = 1.0
scrollImg.maximumZoomScale = 10.0
here is the rest of the code.
var vWidth = self.view.frame.width
var vHeight = self.view.frame.height
var scrollImg: UIScrollView = UIScrollView()
scrollImg.delegate = self
scrollImg.frame = CGRectMake(0, 0, vWidth!, vHeight!)
scrollImg.backgroundColor = UIColor(red: 90, green: 90, blue: 90, alpha: 0.90)
scrollImg.alwaysBounceVertical = false
scrollImg.alwaysBounceHorizontal = false
scrollImg.showsVerticalScrollIndicator = true
scrollImg.flashScrollIndicators()
scrollImg.minimumZoomScale = 1.0
scrollImg.maximumZoomScale = 10.0
defaultView!.addSubview(scrollImg)
imageView!.layer.cornerRadius = 11.0
imageView!.clipsToBounds = false
scrollImg.addSubview(imageView!)
I also had to add this as well
func viewForZoomingInScrollView(scrollView: UIScrollView) -> UIView? {
return self.imageView
}
Swift 3 & above function prototype
func viewForZooming(in scrollView: UIScrollView) -> UIView? {
return self.mainImage
}
The option for swift 4
class ViewController: UIViewController, UIScrollViewDelegate {
@IBOutlet weak var scrolView: UIScrollView!
@IBOutlet weak var imgPhoto: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
scrolView.delegate = self
scrolView.minimumZoomScale = 1.0
scrolView.maximumZoomScale = 10.0
}
func viewForZooming(in scrollView: UIScrollView) -> UIView? {
return imgPhoto
}
}