user4806509 user4806509 - 3 months ago 73
Swift Question

scrollViewDidEndZooming not called while zooming in a UIScrollView

This could possibly be an Xcode bug? I have an

UIImage
inside a
UIScrollView
done programatically in code. The user can zoom into the image using the zoom gesture or double tapping the image. This part all works fine.

Next, I am trying to detect when the scroll view has finished zooming after a zoom gesture or double tap using the below function
scrollViewDidEndZooming
. However,
scrollViewDidEndZooming
never gets called, and doesn't print the line
I just finished zooming!


Code not working:

func scrollViewDidEndZooming(scrollView: UIScrollView, with view: UIView?, atScale scale: CGFloat) {
print("I just finished zooming!")
}



Question:

What might be going on here, why isn't
scrollViewDidEndZooming(...)

getting called?


Note: I've referred to the below document with no luck.
https://developer.apple.com/reference/uikit/uiscrollviewdelegate/1619407-scrollviewdidendzooming




Complete code:

class ScrollViewController: UIViewController, UIScrollViewDelegate {
var scrollView: UIScrollView!
var imageView: UIImageView!

override func viewDidLoad() {
super.viewDidLoad()

imageView = UIImageView(image: UIImage(named: "image.png"))

scrollView = UIScrollView(frame: view.bounds)
scrollView.backgroundColor = UIColor.blackColor()
scrollView.contentSize = imageView.bounds.size
scrollView.autoresizingMask = [UIViewAutoresizing.FlexibleWidth, UIViewAutoresizing.FlexibleHeight]

scrollView.contentOffset = CGPoint(x: 1000, y: 450)

scrollView.addSubview(imageView)
view.addSubview(scrollView)

scrollView.delegate = self
setZoomScale()
setupGestureRecognizer()
}

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

override func viewWillLayoutSubviews() {
setZoomScale()
}

func setZoomScale() {
let imageViewSize = imageView.bounds.size
let scrollViewSize = scrollView.bounds.size
let widthScale = scrollViewSize.width / imageViewSize.width
let heightScale = scrollViewSize.height / imageViewSize.height

scrollView.minimumZoomScale = min(widthScale, heightScale)
scrollView.zoomScale = 1.0
}

func scrollViewDidZoom(scrollView: UIScrollView) {
let imageViewSize = imageView.frame.size
let scrollViewSize = scrollView.bounds.size
let verticalPadding = imageViewSize.height < scrollViewSize.height ? (scrollViewSize.height - imageViewSize.height) / 2 : 0
let horizontalPadding = imageViewSize.width < scrollViewSize.width ? (scrollViewSize.width - imageViewSize.width) / 2 : 0

scrollView.contentInset = UIEdgeInsets(top: verticalPadding, left: horizontalPadding, bottom: verticalPadding, right: horizontalPadding)
}

func scrollViewDidEndZooming(scrollView: UIScrollView, with view: UIView?, atScale scale: CGFloat) {
print("I just finished zooming!")
}

func setupGestureRecognizer() {
let doubleTap = UITapGestureRecognizer(target: self, action: "handleDoubleTap:")
doubleTap.numberOfTapsRequired = 2
scrollView.addGestureRecognizer(doubleTap)
}

func handleDoubleTap(recognizer: UITapGestureRecognizer) {
if (scrollView.zoomScale > scrollView.minimumZoomScale) {
scrollView.setZoomScale(scrollView.minimumZoomScale, animated: true)
} else {
scrollView.setZoomScale(scrollView.maximumZoomScale, animated: true)
}
}
}

Answer

You make a small mistake. Try this code:

    func scrollViewDidEndZooming(scrollView: UIScrollView, withView view: UIView?, atScale scale: CGFloat) {
    print("I just finished zooming!")
}