1

edit: I have updated the question code so that it now works in case it helps anyone.

I have a scrollView in a UICollectionViewCell but can't get the zoom to work in the simulator with pinch. I can get this to happen by zooming the view manually but I need to change the image view frame to full size when the user pinches to zoom. Right now it is size to fill with frame width set to screen width and frame height set to screen height. I need to dynamically change the frame width to equal the frame height as the image is square.

Here is my code:

import UIKit

class MyCollectionViewCell: UICollectionViewCell, UIScrollViewDelegate {

    let screenWidth = UIScreen.main.bounds.width

    let activityIndicator: UIActivityIndicatorView = {
        let spinner = UIActivityIndicatorView()
        spinner.activityIndicatorViewStyle = .white
        spinner.color = Constants.APP_SPINNER_COLOR
        spinner.hidesWhenStopped = true
        return spinner
    }()

    var zoomScrollView: UIScrollView = {
        let scrollView = UIScrollView()
        scrollView.backgroundColor = .white
        scrollView.minimumZoomScale = 1.0
        scrollView.maximumZoomScale = 6.0
        scrollView.clipsToBounds = true
        scrollView.isUserInteractionEnabled = true
        scrollView.translatesAutoresizingMaskIntoConstraints = false
        return scrollView
    }()

    var itemImageView: UIImageView = {
        let imageView = UIImageView()
        imageView.contentMode = .scaleAspectFit
        imageView.translatesAutoresizingMaskIntoConstraints = false
        imageView.backgroundColor = .white
        imageView.isUserInteractionEnabled = true
        return imageView
    }()

    func viewForZooming(in zoomScrollView: UIScrollView) -> UIView? {
        return itemImageView
    }

    override init(frame: CGRect) {
        super.init(frame: frame)

        zoomScrollView.delegate = self

        contentView.backgroundColor = .white

        contentView.addSubview(zoomScrollView)

        zoomScrollView.addSubview(itemImageView)

        itemImageView.addSubview(activityIndicator)


    zoomImageView.frame = CGRect(x:0, y:0, width: screenWidth, height: screenHeight)

    zoomScrollView.frame = itemImageView.frame

        activityIndicator.anchorCenterXToSuperview()
        activityIndicator.anchorCenterYToSuperview()    
    }

    required init?(coder aDecoder: NSCoder) {
            fatalError("init(coder:) has not been implemented")
        }
}
markhorrocks
  • 1,199
  • 19
  • 82
  • 151

2 Answers2

1

You need to set the scrollView.contentSize for pinch zoom to work. See https://developer.apple.com/library/content/documentation/WindowsViews/Conceptual/UIScrollView_pg/CreatingBasicScrollViews/CreatingBasicScrollViews.html

pesch
  • 1,976
  • 2
  • 17
  • 29
  • I set the scroll view content size to be double the screen width and screen height. I can't test this properly on the simulator as it won't pinch expand. Does that sound right? – markhorrocks May 05 '17 at 17:46
1

Here Make simple demo of Scroll View.

Note: Doesn't Give any constraint to image view

Below code for scrollview:

import UIKit

class ViewController: UIViewController,UIScrollViewDelegate {

    var imgDemo: UIImageView = {
        let img = UIImageView()
        img.contentMode = .scaleAspectFill
        img.isUserInteractionEnabled = true
        return img
    }()


    var scrollView:UIScrollView = {
        let scroll = UIScrollView()
        scroll.maximumZoomScale = 4.0
        scroll.minimumZoomScale = 0.25
        scroll.clipsToBounds = true
        return scroll
    }()

    override func viewDidLoad() {
        super.viewDidLoad()
        imgDemo.frame = CGRect(x: 0, y: 0, width: view.frame.size.width, height: view.frame.size.height)
        imgDemo.image = UIImage(named: "5.jpg")
        scrollView.delegate = self
        scrollView.frame = imgDemo.frame
        scrollView.addSubview(imgDemo)
        view.addSubview(scrollView)
    }

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

}
Sakir Sherasiya
  • 1,562
  • 1
  • 17
  • 31
  • I need this view to fit like .scaleAspectfit until I pinch it and then I want it to scaleAspectFill, how can I do that? – markhorrocks May 15 '17 at 16:34
  • Ohk then make it by your self because i don't know how to do that things. – Sakir Sherasiya May 17 '17 at 00:57
  • I have updated the question so my code now works. What was missing before is that I had set the scrollView.userInteraction to false. Thanks for your input re the frames. – markhorrocks May 22 '17 at 18:09