I have a problem for zooming in UIImageView
.
To understand, my UIViewController
have the following structure, very simple :
I just have a collection of images. The screen displays only one image at a time, but to see other images, I can scroll left or right. (Just like the photos app from Apple). I want the zooming feature in these images.
This is how my UIViewController
is working :
I have a main UIScrollView
that contains many UIScrollView
(one per image). Basically in each of these UIScrollView
, there is an UIImageView
. (I didn't use UICollectionView
to allow zoom : I just have many UIScrollView
that contain UIImageView
, to allow zoom in each image).
This is how it looks :
This is how I did it :
var counter : Int = 0
let scrollViewWidth:CGFloat = scrollView.frame.width
let scrollViewHeight:CGFloat = scrollView.frame.height
// Add images to scrollview
for object in myObjects {
// Configure the UIImageView
let imgView = UIImageView(image: object.image.value)
imgView.frame = CGRect(x:0, y:0, width:scrollViewWidth, height:scrollViewHeight)
imgView.contentMode = .scaleAspectFit
imgView.isUserInteractionEnabled = true
if let rotation = viewModel?.documents?[counter].rotation {
imgView.transform = CGAffineTransform(rotationAngle: rotation)
}
// Configure a UIScrollView for each UIImageView to allow both zooming and paging
let pageScrollView = UIScrollView(frame: CGRect(x:scrollViewWidth * CGFloat(counter), y:0, width:scrollViewWidth, height:scrollViewHeight))
pageScrollView.minimumZoomScale = 1.0
pageScrollView.maximumZoomScale = 2.0
pageScrollView.contentSize = imgView.bounds.size
pageScrollView.showsVerticalScrollIndicator = false
pageScrollView.showsHorizontalScrollIndicator = false
pageScrollView.addSubview(imgView)
self.scrollView.addSubview(pageScrollView)
counter += 1
}
// Set content size for the main UIScrollView
self.scrollView.contentSize = CGSize(width:self.scrollView.frame.width * CGFloat((viewModel?.documents)!.count), height:self.scrollView.frame.height)
Obviously I implemented the viewForZooming
method : the method is called, the method returns the right UIImageView, but nothing happens when I pinch to zoom :
/// Zoom in / out for the current UIImageView
public func viewForZooming(in scrollView: UIScrollView) -> UIView? {
// self.scrollView : main UIScrollView
if scrollView == self.scrollView {
if let childScrollView = self.scrollView.subviews[currentPageIndex] as? UIScrollView {
if let currentImageView = childScrollView.subviews[0] as? UIImageView {
return currentImageView // the UIImageView is well returned
}
}
}
return nil
}
I don't understand, if anyone have an idea ?
Thanks.