Akmal
Akmal

Reputation: 7

Reset Scrollview in UICollectionViewCell after scrolling

I'm creating a photo gallery with this concept. For listing the images, I've used UICollectionView. Each image is stored in full screen size custom cells. Inside the cell, I have UIScrollView and inside it I have UIImageView. ScrollView is used to zoom the image. All working fine but, when I zoom in one image and scroll to another cell without zooming out, I want that previous cell's scrollview to be reset.

Inside custom cell class, I set ZoomScale for each cell's scrollview when they are initiated like so:

func configureCell(_ photo: String){
    albumPhoto.image = UIImage(named: photo)
    scrollView.setZoomScale(1.0, animated: false)
}

I configure each cell in cellForItemAt function before returning the cell.

When I zoom in in the first cell, scroll to second and scroll back to the first cell, the image is still zoomed in. But if I scroll to second and third cell and then return to first cell, the image is zoomed out to default.

How can I achieve that even after scrolling immediately back from second cell to first, the scroll view will be set to default zoom scale.

Upvotes: 0

Views: 1040

Answers (2)

dvp.petrov
dvp.petrov

Reputation: 1120

you need to implement this method: https://developer.apple.com/documentation/uikit/uicollectionviewdelegate/1618087-collectionview and zoom out the scrollView before displaying the cell. This method will handle your case. Happy coding ;)

func collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath)

Upvotes: 1

sheko
sheko

Reputation: 556

First make your class conforms to

class profViewController: UIScrollViewDelegate

As UICollectionView is a subclass of UIScrollView

Second implement this delegate method

  func scrollViewDidScroll(_ scrollView: UIScrollView)
  { 

        let cd = areaSettTable.visibleCells as! 

       [profTableViewCell]

       //////

       loop here

  }

then loop through this array and call the function that resets the zoom for every cell

note : you should change profTableViewCell name to your cell

Upvotes: 1

Related Questions