collectionView 的 didSelectItemAt 与 UITapGestureRecognizer 一起使用时未调用
collectionView's didSelectItemAt not called when use it with UITapGestureRecognizer
一旦我使用 addGestureRecognizer 关闭滚动视图中的键盘,collectionView 的 didSelectItemAt 将无法工作。有什么建议吗?
更新代码:目前我可以点击关闭键盘并点击对收集单元格进行操作。但是,如果我滑动 scrollView,键盘就会关闭。有什么办法可以防止这种情况发生?
class PostVC: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate {
@IBOutlet weak var colorCollectionView: UICollectionView!
@IBOutlet weak var scrollView: UIScrollView!
@IBOutlet weak var titleTextField: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
let tapViewGesture = UITapGestureRecognizer(target: self, action: #selector(PostVC.didTapViewForDismissKeyboard))
scrollView.addGestureRecognizer(tapViewGesture)
tapViewGesture.delegate = self
}
func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldReceive touch: UITouch) -> Bool{
view.endEditing(true)
return false
}
func didTapViewForDismissKeyboard(_ pressed: UIGestureRecognizer) {
view.endEditing(true)
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
print("HIHI")
}
extension PostVC: UIGestureRecognizerDelegate {
func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
return true
}
}
不要使用手势识别器。那就是拦截您的点击,而不是将它们提供给集合视图。而是在 collectionView(_:didSelectItemAt:)
方法中调用 view.endEditing(true)
。
尝试实施UIGestureRecognizerDelegate
。在你的代码中实现它的 gestureRecognizer(_:shouldRecognizeSimultaneouslyWith:)
方法到 return true
- 这样你的手势识别器就会工作,但它也会允许识别其他手势(特别是 collectionView
).
代码:
// add this to initializing code to set gesture recognizer's delegate to self
tapViewGesture.delegate = self
委托实施:
extension YourViewController: UIGestureRecognizerDelegate {
func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
return true
}
}
一旦我使用 addGestureRecognizer 关闭滚动视图中的键盘,collectionView 的 didSelectItemAt 将无法工作。有什么建议吗?
更新代码:目前我可以点击关闭键盘并点击对收集单元格进行操作。但是,如果我滑动 scrollView,键盘就会关闭。有什么办法可以防止这种情况发生?
class PostVC: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate {
@IBOutlet weak var colorCollectionView: UICollectionView!
@IBOutlet weak var scrollView: UIScrollView!
@IBOutlet weak var titleTextField: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
let tapViewGesture = UITapGestureRecognizer(target: self, action: #selector(PostVC.didTapViewForDismissKeyboard))
scrollView.addGestureRecognizer(tapViewGesture)
tapViewGesture.delegate = self
}
func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldReceive touch: UITouch) -> Bool{
view.endEditing(true)
return false
}
func didTapViewForDismissKeyboard(_ pressed: UIGestureRecognizer) {
view.endEditing(true)
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
print("HIHI")
}
extension PostVC: UIGestureRecognizerDelegate {
func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
return true
}
}
不要使用手势识别器。那就是拦截您的点击,而不是将它们提供给集合视图。而是在 collectionView(_:didSelectItemAt:)
方法中调用 view.endEditing(true)
。
尝试实施UIGestureRecognizerDelegate
。在你的代码中实现它的 gestureRecognizer(_:shouldRecognizeSimultaneouslyWith:)
方法到 return true
- 这样你的手势识别器就会工作,但它也会允许识别其他手势(特别是 collectionView
).
代码:
// add this to initializing code to set gesture recognizer's delegate to self
tapViewGesture.delegate = self
委托实施:
extension YourViewController: UIGestureRecognizerDelegate {
func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
return true
}
}