簡體   English   中英

gestureRecognizerShouldBegin 塊 collectionViewCell

[英]gestureRecognizerShouldBegin blocks the collectionViewCell

我在collectionViewCell上有一個UIPanGestureRecognizer

let swipeToRight = UIPanGestureRecognizer(target: self, action: #selector(panHandler))
 swipeToRight.minimumNumberOfTouches = 1
 swipeToRight.maximumNumberOfTouches = 1
 swipeToRight.delegate = self
 container.addGestureRecognizer(swipeToRight)

但它會中斷滾動,所以我也使用gestureRecognizerShouldBegin讓我滾動。

   public override func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {

    if let pan = gestureRecognizer as? UIPanGestureRecognizer {
        let velocity = pan.velocity(in: self)
        return abs(velocity.x) > abs(velocity.y)
    }
    return true
}

它運行良好,但正如您在視圖層次結構的屏幕截圖中看到的那樣,對於一些隨機單元格,它在collectionViewCell的頂部添加了一個手勢識別器,它阻止了單元格上的每個按鈕。 我從視圖層次結構中打印了視圖描述。

Printing description of $33:
<UIView: 0x10b678d30; frame = (0 0; 410 357.667); gestureRecognizers = <NSArray: 0x280800660>; layer = <CALayer: 0x280484ce0>>

如果我使用gestureRecognizerShouldBegin ,我將遇到完全相同的問題

  public override func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {

    if let pan = gestureRecognizer as? UIPanGestureRecognizer {
        let velocity = pan.velocity(in: self)
        return abs(velocity.x) > abs(velocity.y)
    }
    return true
}

這是屏幕截圖

視圖層次結構的屏幕截圖

例如,這是另一個單元格的視圖層次結構的屏幕截圖,它按預期工作並且沒有任何阻塞視圖

在此處輸入圖像描述

這幾天我一直被這個問題困擾,誰能幫我解決一下? 我什至能干地刪除我的UIPanGestureRecognizer ,但gestureRecognizerShouldBegin仍然對一些隨機單元格造成這個問題。 非常感謝您的幫助

既然你提到這個問題只發生在幾個單元格而不是其他單元格中,我假設這與單元格內的視圖如何被添加為子視圖有關。

我建議您檢查是否在 UICollectionViewCell 實例的contentView中添加視圖,而不是將視圖直接添加到不推薦的單元格中 這可能是導致此問題的原因之一,但考慮到詳細信息,我不能肯定地說。

如果以上不是問題,請附上一個最小的、可重現的示例來演示問題,以便我能夠更詳細地了解這個問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

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