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.