0
votes

I have the following hierarchy: UIViewController -> UICollectionView -> UICollectionViewCell -> UIScrollView -> UIImageView -> UIImage

With this hierarchy, I can have working-well zooming capability. But when I am done zooming-in, and swipe in to the next image, I need the previous imageview to reset to its original scale.

My Attempt: I stored the current index in my collection into

self.currendIndex

and then tried getting the collection view cell and set its scrollview zooming like so in the scrollViewWillBeginDecelerating event inside the view controller

    func scrollViewWillBeginDecelerating(_ scrollView: UIScrollView) {
        let indexPath = IndexPath(item: self.currendIndex, section: 0)
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath) as! ImageCollectionViewCell
        DispatchQueue.main.async {
            cell.scrollView.zoomScale = 1
            cell.scrollView.contentOffset = CGPoint(x: 0, y: 0)
        }
    }

But it doesn't zoom it down, it just goes to the next element and then when I come back it is still with the zoom level.

How to fix it?

1
Try without the async. - sunkehappy
@sunkehappy thank you for ur comment, but it still didn't work with removing that async - user1019042

1 Answers

0
votes

I fixed this by using the reloadItems method:

func scrollViewWillBeginDecelerating(_ scrollView: UIScrollView) {
    let indexPath = IndexPath(item: self.currendIndex, section: 0)

    DispatchQueue.main.async(execute: {() -> Void in
        self.collectionView.reloadItems(at: [indexPath])
    })
}

and it had to be inside the async