1

I have a UICollectionView, which has its cells, as normally.

Each cell has itself a UICollectionViewController within.

When I tap a cell, I expand it to fit the window screen, but I've encountered some problems here:

If I scroll within the expanded cell , the whole screen scrolls;

What I'm trying to do it to block the superview collection view taps and scrolls.

In order to do so, after expanding the selected cell to fit the desired frame, I set collectionView.isScrollEnabled = false and collectionView.isUserInteractionEnabled = false , but with this I came across another problem which is: The expanded cell doesn't interact with any touch.

How can I achieve this? Expand the cell and block the scrollview scroll , block scrollview interaction , but let the cell interaction enabled (scroll taps, etc)?

Here's some of the code:

Main Feed Controller

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    guard let cell = collectionView.cellForItem(at: indexPath) as? FeedCell else { return }
    let theAttributes:UICollectionViewLayoutAttributes! = collectionView.layoutAttributesForItem(at: indexPath)
    let cellFrameInSuperview:CGRect = collectionView.convert(theAttributes.frame, to: collectionView)

    cell.superview?.bringSubview(toFront: cell)
    cell.backupFrame = cellFrameInSuperview

    let newFrame = CGRect(x: 0, y: collectionView.contentOffset.y, width: view.frame.width, height: view.frame.height)
    UIView.animate(withDuration: 0.2, animations: {
        cell.transform = CGAffineTransform(scaleX: 0.95, y: 0.95)
    }) { (_) in
        UIView.animate(withDuration: 0.1, animations: {
            cell.transform = CGAffineTransform(scaleX: 1, y: 1)
        }, completion: { (_) in
            UIView.animate(withDuration: 0.8, delay: 0, usingSpringWithDamping: 0.6, initialSpringVelocity: 0.7, options: .curveEaseInOut, animations: {
                cell.frame = newFrame                   
            },completion: {(_) in
                cell.presentDismissButton()
                self.collectionView.isScrollEnabled = false
                self.collectionView.isUserInteractionEnabled = false
            })
        })                      
    }
}

Expanded Cell dismiss button property

@objc func dismiss() {
    UIView.animate(withDuration: 0.8, delay: 0, usingSpringWithDamping: 0.8, initialSpringVelocity: 1, options: .allowAnimatedContent, animations: {
        self.frame = self.backupFrame!
        self.sendSubview(toBack: self)
        self.dismissButton.alpha = 0
    },completion: nil) 
}

In the dismiss property I can set the superview interaction as enabled again, but with the expanded frame I can't interact with it.

One problem I have here if I don't disable the Main feed collectionView interaction is that when I tap anywhere on the expanded cell it will expand another cell (which is behind my expanded one, which will receive the touch event from didSelectItem(:_)

Any hint? Thank you.

Ivan Cantarino
  • 3,058
  • 4
  • 34
  • 73
  • You could try adding a subview behind your cell like this guy: https://stackoverflow.com/a/5404921/8374624 – Jakob Mygind Oct 10 '17 at 12:37
  • @JakobMygind yes it is as well a nice approach. What I just remembered as well was to add something like `hasCellExpanded = false` and when a cell is expanded I set it to true, and on the `didSelectItemAtIndexPath(_:)` if the `hasCellExpanded == true` I simply return. – Ivan Cantarino Oct 10 '17 at 12:42

1 Answers1

0

If you set userInteractionEnabled = false then you cannot interact with any subviews. Is it not enough to just disable scrolling?

e.g. try removing self.collectionView.isUserInteractionEnabled = false from your completion block?

  • I’ve tried that, but that causes another issue. As I’m expanding the cell to fit the frame, it will be on top of other cells and if click in the center of the expanded cell, it could fire another ‘didSelectItem’ from the superview – Ivan Cantarino Oct 10 '17 at 12:35
  • Ohh... I just remembered I can create a property that returns on any cell touch if one is expanded. – Ivan Cantarino Oct 10 '17 at 12:36