簡體   English   中英

如何禁用CollectionView的垂直手勢識別器?

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM