user4806509 user4806509 - 1 year ago 287
Swift Question

scrollViewDidEndZooming not called while zooming in a UIScrollView

This could possibly be an Xcode bug? I have an

inside a
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
. However,
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!")


What might be going on here, why isn't

getting called?

Note: I've referred to the below document with no luck.

Complete code:

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

override func 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.delegate = self

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

override func viewWillLayoutSubviews() {

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

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

Answer Source

You make a small mistake. Try this code:

    func scrollViewDidEndZooming(scrollView: UIScrollView, withView view: UIView?, atScale scale: CGFloat) {
    print("I just finished zooming!")
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download