Add a single tap gesture to UICollectionView
, do not get in the way of cell selection.
I want some other taps on the no-cell part of the collectionView.
Using XCode8, Swift 3.
override func viewDidLoad() { ... collectionView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(tap))) } func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { print(indexPath) } func tap(sender: UITapGestureRecognizer){ print("tapped") }
Yeah, it gets in the way now. When you tap on cell, it logs "tapped".
Couldn't find more trace. Any ideas on how cell selection is implemented or to achieve this task?
Tap and swipe are two common gestures that allow the user to perform primary actions on their mobile devices. The tap gesture is essentially a brief touch of the mobile screen surface with the fingertip. Common uses of this gesture in iOS and Android devices include: Select or submit. Activate.
The iOS UITapGestureRecognizer class has a built-in way to detect a double tap on any view. All you need to do is create the recognizer, set its numberOfTapsRequired property to 2, then add it to the view you want to monitor.
How to use? Just inherit BaseCollectionViewCell . If needed, configure in cell's init or collectionView 's delegate methods. If you don't need highlight effect, just find a method named 'shouldHighlightItemAtIndexPath' in UICollectionViewDelegate and return false or just set cell.
Whenever you want to add a gesture recognizer, but not steal the touches from the target view, you should set UIGestureRecognizer.cancelsTouchesInView
for your gestureRecognizer
instance to false.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With