markhorrocks
markhorrocks

Reputation: 1528

Swift3 Zoom how to change UICollectionView frame size on pinch?

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")
        }
}

Upvotes: 1

Views: 762

Answers (2)

Sakir Sherasiya
Sakir Sherasiya

Reputation: 1572

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
    }

}

Upvotes: 1

Related Questions