Alex Rabin Alex Rabin - 1 month ago 9
iOS Question

Scrollview won't zoom

Everything works perfectly except that i cant zoom in on the imageview.

var zoomingImageView = UIImageView()
var startingFrame : CGRect?
var blackBackground : UIScrollView?
var startingImageView: UIImageView?


this is where i set up the scrollview and the imageview inside of it

func performZoomingForStartImageView(imageView: UIImageView){


if let keyWindow = UIApplication.shared.keyWindow {

blackBackground = UIScrollView(frame: CGRect(x: self.view.frame.origin.x, y: self.view.frame.origin.y, width: self.view.frame.size.width, height: self.view.frame.size.height))
blackBackground?.delegate = self
blackBackground?.maximumZoomScale = 4.0
blackBackground?.minimumZoomScale = 1.0
blackBackground?.isScrollEnabled = true
blackBackground?.backgroundColor = .black
blackBackground?.alpha = 0.0
blackBackground?.isUserInteractionEnabled = true
blackBackground?.contentSize = CGSize(width: 0, height: self.view.frame.size.height)
keyWindow.addSubview(blackBackground!)

self.startingImageView = imageView
self.startingImageView?.isHidden = true
startingFrame = imageView.superview?.convert(imageView.frame, to: nil)

zoomingImageView = UIImageView(frame: startingFrame!)
zoomingImageView.image = startingImageView?.image
zoomingImageView.contentMode = .scaleAspectFit
zoomingImageView.isUserInteractionEnabled = true
zoomingImageView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(self.handleZoomOut)))
zoomingImageView.addGestureRecognizer(UILongPressGestureRecognizer(target: self, action: #selector(self.saveImage)))
blackBackground?.addSubview(zoomingImageView)

UIView.animate(withDuration: 0.5, delay: 0, usingSpringWithDamping: 1, initialSpringVelocity: 1, options: [.curveEaseOut,.allowUserInteraction], animations: {
self.blackBackground?.alpha = 1.0
self.inputContainerView.alpha = 0
//let height = startingFrame!.height / startingFrame!.height * keyWindow.frame.widthimagedShown
self.zoomingImageView.frame = CGRect(x: self.view.frame.origin.x, y: self.view.frame.origin.y, width: self.view.frame.size.width, height: self.view.frame.size.height)
self.zoomingImageView.center = keyWindow.center

}, completion: nil)

}


}


this is supposed to allow the zooming but the print never gets called and i have set up the delegate

func viewForZoomingInScrollView(scrollView: UIScrollView) -> UIView? {
print("hi")
return zoomingImageView
}

Answer

For Swift 3 the method signature has changed.

Swift 3:

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