0
votes

I have UIImageView as a subView of UIScrollView.

let newImageView = UIImageView(image: imageView.image)

newImageView.contentMode = .scaleAspectFit
newImageView.isUserInteractionEnabled = true
let tap = UITapGestureRecognizer(target: self, action: #selector(self.dismissFullscreenImage))
newImageView.addGestureRecognizer(tap)

scrollPinch = UIScrollView()
scrollPinch.frame = UIScreen.main.bounds
scrollPinch.isUserInteractionEnabled = true
scrollPinch.minimumZoomScale = 1.0
scrollPinch.maximumZoomScale = 6.0
scrollPinch.delegate = self
scrollPinch.clipsToBounds = true

newImageView.frame = scrollPinch.bounds

self.view.addSubview(self.scrollPinch)
self.scrollPinch.addSubview(newImageView)

Like this. (* scrollPinch is declared as var scrollPinch: UIScrollView! outside of the method in this ViewController class) Then, in the delegate method,

func viewForZooming(in scrollView: UIScrollView) -> UIView? {
    if (scrollView == scrollPinch){
        if (scrollView.subviews[0].isKind(of: UIImageView.self)){

            return scrollView.subviews[0]
        }
    }
    return nil
}

Now, I try zooming in at first, the imageView moves to the right and not zoom. However, after I let go of imageView and try zooming in again, it works perfectly.

Any ideas how to solve this??

1

1 Answers

0
votes

It turned out that I should have done

self.scrollPinch.addSubview(newImageView)
self.view.addSubview(self.scrollPinch)

instead of

self.view.addSubview(self.scrollPinch)
self.scrollPinch.addSubview(newImageView)

It all worked out after I swap those two lines.