LXReorderableCollectionViewFlowLayout icon indicating copy to clipboard operation
LXReorderableCollectionViewFlowLayout copied to clipboard

Uninstall existing gesture recognizers when calling `setupCollectionView`

Open acruis opened this issue 6 years ago • 2 comments

There is a behaviour on the current iOS version (11.4, not sure when this behaviour started) where if a collection view's layout is set after initialization - instead of it being passed inside init(frame:collectionViewLayout:) - the collectionView property will be set twice.

That is, if you do this

let collectionView = UICollectionView(
    frame: .zero,
    collectionViewLayout: UICollectionViewFlowLayout()
)
collectionView.collectionViewLayout = LXReorderableCollectionViewFlowLayout()

instead of this

let collectionView = UICollectionView(
    frame: .zero,
    collectionViewLayout: LXReorderableCollectionViewFlowLayout()
)

KVO will cause setupCollectionView to be called twice. Consequently the stored panGestureRecognizer is replaced with a new one, but the old one is still listening to gestures on the collection view.

This breaks scrolling, because scrolling over the collection view when self.selectedItemIndexPath is nil should not trigger handlePanGesture, but it does because this

- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer {
    if ([self.panGestureRecognizer isEqual:gestureRecognizer]) {
        return (self.selectedItemIndexPath != nil);
    }
    return YES;
}

returns true for the replaced (but still listening) pan gesture recognizer.

acruis avatar Jun 29 '18 10:06 acruis

I'm seeing this too, what's your solution @acruis ?

ryderjack avatar Jul 23 '18 09:07 ryderjack

For now I'm working around it by only injecting the layout in the init (basically the snippet below "instead of this").

If you have to set the collectionViewLayout manually then...I'm not sure there's much you can do. The layout does expose its gesture recognizers but setupCollectionView sets the ivars directly instead of calling the property setters, which means you can't rely on KVO.

Off the top of my head I have some wild hacks, but none of them are that nice:

class CustomFlowLayout: LXReorderableCollectionViewFlowLayout {
    var settingCollectionView: Bool = false
    override var collectionView: UICollectionView? {
        let collectionView = super.collectionView
        if let gestureRecognizer = self.panGestureRecognizer, self.settingCollectionView {
            collectionView?.removeGestureRecognizer(gestureRecognizer)
        }
        return collectionView
    }
}

...

// (when setting the flow layout)
let reorderableFlowLayout = CustomFlowLayout()
reodrerableFlowLayout.settingCollectionView = true
collectionView.collectionViewLayout = reorderableFlowLayout
reorderableFlowLayout.settingCollectionView = false

I haven't tried this myself, so I'm not sure if it even works though.

  • You might also be able to subclass UICollectionView to override the addGestureRecognizer call to somehow keep track of existing gesture recognizers and remove stale ones accordingly, but if you go that far...
  • ...you might as well just make copies of the collection view and switch between them when you need to change the layout.

acruis avatar Jul 23 '18 15:07 acruis