[英]How to disable CollectionView's vertical gesture recognizer?
我正在使用LNPopupController框架來呈現視圖控制器,可以通過向下拖動來關閉呈現的VC,但是我在此VC中添加了集合視圖,並且在其區域中拖動手指不會將VC向下拖動。 我希望此CollectionView水平滾動,我嘗試對UICollectionView進行子類化並使用
gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool
方法,但沒有任何區別。 這是我的代碼:
class MyCollectionView: UICollectionView, UIGestureRecognizerDelegate {
func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
return true
}
}
在viewDidLoad()
func setupCollection() {
cellSize = CGSize(width: self.view.frame.width, height: self.view.frame.width)
collectionView.delegate = self
collectionView.dataSource = self
if let layout = collectionView.collectionViewLayout as? UICollectionViewFlowLayout {
layout.scrollDirection = .horizontal
}
//collectionView.isScrollEnabled = false
collectionView.isPagingEnabled = true
collectionView.alwaysBounceVertical = false
}
禁用滾動后,一切都將正常運行,我可以通過拖動CollectionView的區域來關閉視圖。
首先,我將在包含集合視圖的視圖控制器中實現UIGestureRecognizerDelegate
:
extension MyViewController: UIGestureRecognizerDelegate {
func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
return true
}
}
然后,您還需要將此委托設置為適當的手勢識別器-在您的代碼中,我看不到任何將委托分配給任何手勢識別器的行。 我將其分配給負責關閉viewController的手勢識別器:
self.popupContentView.popupInteractionGestureRecognizer.delegate = self
在MyViewController
viewDidLoad
中調用此行代碼(我使用此名稱,因為您沒有提及控制器的名稱)。 在這里,我假設popupContentView.popupInteractionGestureRecognizer
是關閉視圖控制器的平移手勢識別器(我迅速檢查了LNPopupController )。
LNPopupController
作者在這里。 另一個答案是正確的。 您需要實現手勢識別器的委托並將其從交互中排除(或者做更復雜的事情)。
為此,請設置controller.popupContentView.popupInteractionGestureRecognizer.delegate
屬性。 確保controller
是您調用presentPopupBarWithContentViewController:animated:completion:
on的實際控制器。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.