IOS: adding a UITapGestureRecognizer to intercept the container view UICollectionView didSelectItemAtIndexPath method

I added a UITapGestureRecognizer to my main Content View in my ViewController to reject my keyboard while listening to the content.

The problem is that I have a UICollectionView inside my content view, and setting UITapGestureRecognizer intercepts the UICollectionView my UICollectionView .

How to allow UICollectionView tags to scroll so that the didSelectItemAtIndexPath method didSelectItemAtIndexPath again?

 func setupGestureRecognizer() { let dismissKeyboardTap = UITapGestureRecognizer(target: self, action: "dismissKeyboard") contentView.addGestureRecognizer(dismissKeyboardTap) } func dismissKeyboard() { contentView.endEditing(true) } 
+6
source share
2 answers

A way to solve this problem is to add .cancelsTouchesInView = false to the UITapGestureRecognizer .

This allows you to touch other views, for example, UITableViewCell touch.

 func setupGestureRecognizer() { let dismissKeyboardTap = UITapGestureRecognizer(target: self, action: "dismissKeyboard") dismissKeyboardTap.cancelsTouchesInView = false contentView.addGestureRecognizer(dismissKeyboardTap) } func dismissKeyboard() { contentView.endEditing(true) } 
+15
source

try it

 - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { [self.view endEditing:YES]; } 

and delete your tapGesture.

0
source

Source: https://habr.com/ru/post/980191/


All Articles