滑动单元格内部的 UISlider 时如何防止单元格滑动

How to prevent a Cell from swiping when the UISlider inside of it is swiped

我有一个水平的 CollectionViewCell,里面有一个 UISlider。当我尝试拖动滑块时,单元格会滑动。

拖动滑块时如何防止单元格滑动?我不想失去滑动单元格本身的能力,意思是当我滑动单元格时应该移动到与滑块无关的下一个单元格。

class MyCell: UICollectionViewCell {

    lazy var slider: UISlider = {
        let slider = UISlider()
        slider.translatesAutoresizingMaskIntoConstraints = false
        // ...
        return slider
    }()


    // slider is pinned towards the bottom of the cell
}

得到答案from here which got the answer from here:

override init(frame: CGRect) {
    super.init(frame: frame)

    let panGesture = UIPanGestureRecognizer(target: nil, action:nil)
    panGesture.cancelsTouchesInView = false
    slider.addGestureRecognizer(panGesture)
}