iOS: Adding UITapGestureRecognizer to container view intercepts UICollectionView's didSelectItemAtIndexPath method

亡梦爱人 提交于 2020-01-10 19:27:09

问题


I added a UITapGestureRecognizer to my main Content View in my ViewController to dismiss my keyboard when the content view is tapped.

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

How do I allow my UICollectionView's taps to go through so that the didSelectItemAtIndexPath method will fire again?

func setupGestureRecognizer() {
    let dismissKeyboardTap = UITapGestureRecognizer(target: self, action: "dismissKeyboard")
    contentView.addGestureRecognizer(dismissKeyboardTap)
}

func dismissKeyboard() {
    contentView.endEditing(true)
}

回答1:


The way to solve this issue is by adding .cancelsTouchesInView = false to your UITapGestureRecognizer.

This allows touches inside other views to go through, such as a UITableViewCell touch.

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

func dismissKeyboard() {
    contentView.endEditing(true)
}



回答2:


try this

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

and remove your tapGesture.



来源:https://stackoverflow.com/questions/27664056/ios-adding-uitapgesturerecognizer-to-container-view-intercepts-uicollectionview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!