问题描述
我在我的ViewController中向我的主内容视图
添加了一个 UITapGestureRecognizer
,以便在内容视图时关闭我的键盘轻拍。
I added a UITapGestureRecognizer
to my main Content View
in my ViewController to dismiss my keyboard when the content view is tapped.
问题是我的内容视图中有一个 UICollectionView
,并设置 UITapGestureRecognizer
拦截我的 UICollectionView
的点击。
The problem is that I have a UICollectionView
inside my content view, and setting the UITapGestureRecognizer
intercepts the taps of my UICollectionView
.
我如何允许我的 UICollectionView
点击一下,以便再次触发 didSelectItemAtIndexPath
方法?
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)
}
推荐答案
解决此问题的方法是添加 .cancelsTouchesInView = false
到你的 UITapGestureRecognizer
。
The way to solve this issue is by adding .cancelsTouchesInView = false
to your UITapGestureRecognizer
.
这允许其他视图中的触摸通过,例如a UITableViewCell
触摸。
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)
}
这篇关于iOS:向容器视图添加UITapGestureRecognizer拦截UICollectionView的didSelectItemAtIndexPath方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!