swiftuicollectionviewcelluipangesturerecognizer

gestureRecognizerShouldBegin blocks the collectionViewCell


I have a UIPanGestureRecognizeron a collectionViewCell

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

but it interrupts scrolling, so I use gestureRecognizerShouldBegin to let me scrolling as well.

   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
}

And it works well, but as you can see in the screenshot from the view hierarchy, for some random cell, it added a gesture recognizer in the top of the collectionViewCelland it blockes every button on the cell. I printed the view description from the view hierarchy.

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

if I use gestureRecognizerShouldBegin, I will have exactly the same problem

  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
}

Here is the screenshot

the screenshot from the view hierarchy

And for example, here is the screenshot from the view hierarchy for an another cell that works as expected and there is no any blocking view

enter image description here

I have been stuck with this issue in the past few days, could anyone help me that how I can fix it? I even competently remove my UIPanGestureRecognizer, but gestureRecognizerShouldBegin still makes that issue for some random cell. Your help will be so highly appreciated


Solution

  • Since you mention the issue occurs only in a few cells and not in others, I'm assuming this has something to do with how the views within the cell are being added as subviews.

    I would recommend you check if you are adding your views within the contentView of the UICollectionViewCell instance instead of adding views directly into the cell which is not recommended. This could be one reason for this issue although, given the details, I can't say for sure.

    If the above is not it, please attach an Minimal, Reproducible Example that demonstrates the issue so I'm able to look into this with more detail and context.