gpt4 book ai didi

ios - 与 UITapGestureRecognizer 一起使用时,collectionView 的 didSelectItemAt 不会被调用

转载 作者:行者123 更新时间:2023-11-29 00:11:56 24 4
gpt4 key购买 nike

一旦我使用addGestureRecognizer在scrollView中关闭键盘,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
}
}

最佳答案

尝试实现UIGestureRecognizerDelegate .在你的代码中实现它的 gestureRecognizer(_:shouldRecognizeSimultaneouslyWith:) 方法以返回 true - 这样你的手势识别器就会工作,但它也会允许识别其他手势(特别是 collectionView 中的那个)。

代码:

// add this to initializing code to set gesture recognizer's delegate to self
tapViewGesture.delegate = self

委托(delegate)实现:

extension YourViewController: UIGestureRecognizerDelegate {
func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
return true
}
}

关于ios - 与 UITapGestureRecognizer 一起使用时,collectionView 的 didSelectItemAt 不会被调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46306644/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com