iOS Development

ios – Zoom CollectionView inside ScrollView

Spread the love


I need to zoom a collectionView inside a scrollView, however after I zoom the scrollView, the contentOffset of the scrollView would not change. Subsequently, I can not scroll to the highest of the collectionView when zoomed. How can I tackle this example? I need to easily zoom and scroll, much like studying a PDF.

Right here is my code

import UIKit

class ViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate {
    
    static let cellId = "CellId"

    var collectionView: UICollectionView?
    var scrollView: UIScrollView?
    override func viewDidLoad() {
        tremendous.viewDidLoad()
        let structure = UICollectionViewFlowLayout()
        structure.itemSize = CGSize(width: view.body.width, peak: view.body.peak)
        structure.scrollDirection = .vertical
        structure.minimumLineSpacing = 0
        structure.minimumInteritemSpacing = 0
        structure.sectionInset = UIEdgeInsetsMake(0,0,0,0)



        collectionView = UICollectionView(body: CGRect(origin: .zero, measurement: self.view.body.measurement), collectionViewLayout: structure)

        collectionView!.register(ZoomableCell.self, forCellWithReuseIdentifier: "singleCell")
        collectionView!.delegate = self
        collectionView!.dataSource = self
        scrollView = UIScrollView(body: self.view.body)
        self.view.addSubview(scrollView!)
        scrollView!.addSubview(collectionView!)
        scrollView!.delegate = self
        scrollView!.maximumZoomScale = 3
        let doubleTapGestureRecognizer = UITapGestureRecognizer(goal: self, motion: #selector(doubleTapGesture(_:)))
        doubleTapGestureRecognizer.numberOfTapsRequired = 2
        collectionView!.addGestureRecognizer(doubleTapGestureRecognizer)

    }
    @objc
    personal func doubleTapGesture(_ sender: UITapGestureRecognizer) {
        if ( self.scrollView!.zoomScale > 1.0) {
            self.scrollView!.setZoomScale(1.0, animated: true)
        } else {
            let zoomRect:CGRect = self.zoomRectForScale(scale: 3.0 / 2 , heart: sender.location(in: sender.view))
            self.scrollView!.zoom(to: zoomRect, animated: true)
        }

    }

    func zoomRectForScale(scale:CGFloat, heart: CGPoint) -> CGRect{
        var zoomRect: CGRect = CGRect()
        // this level (heart) is relative to assortment view, want convert again to contentView
        let contentW = view.body.width
        let contentH = view.body.peak
        let newX = heart.x - contentW * flooring(heart.x / contentW)
        let newPoint = CGPoint(x: newX, y: heart.y)
        zoomRect.measurement.peak = contentH / scale
        zoomRect.measurement.width = contentW / scale
        zoomRect.origin.x = newPoint.x - zoomRect.measurement.width / 2.0
        zoomRect.origin.y = newPoint.y - zoomRect.measurement.peak / 2.0
        return zoomRect
    }

    override func didReceiveMemoryWarning() {
        tremendous.didReceiveMemoryWarning()
        // Get rid of any sources that may be recreated.
    }
    
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 1
    }
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection part: Int) -> Int {
        return 10
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell:ZoomableCell = collectionView.dequeueReusableCell(withReuseIdentifier: "singleCell", for: indexPath) as! ZoomableCell

        let picture = UIImage(named:"take a look at")
        cell.imageView.picture = picture
        return cell
    }

}

extension ViewController: UIScrollViewDelegate {
    func viewForZooming(in scrollView: UIScrollView) -> UIView? {
        return collectionView
    }
}

class ZoomableCell: UICollectionViewCell {
    var imageView:UIImageView!

    required init(coder aDecoder:NSCoder){
        tremendous.init(coder: aDecoder)!
    }

    override init(body:CGRect){
        tremendous.init(body:body)

        setup()
    }

    func setup() {
        //imageViewを生成
        imageView =  UIImageView()
        imageView.body = CGRect(x:0,y:0,width:self.body.width,peak:self.body.peak)
        contentView.addSubview(imageView)

    }

}


Leave a Reply

Your email address will not be published. Required fields are marked *