CollectionViewCell中的UILabel未阻止触摸事件

时间:2017-05-18 18:46:22

标签: ios swift cocoa-touch uikit

我有什么

  • A是UICollectionView
  • B是UICollectionViewCell
  • C是B

    UILabel子视图
    +------------------------------+
    |A                             |
    |                              |
    | +----------+    +----------+ |
    | |C         |    |B         | |
    | |----------|    |          | |
    | |          |    |          | |
    | |B         |    |          | |
    | +----------+    +----------+ |
    +------------------------------+
    

点击B时,C作为子视图添加到单元格的顶部。当再次敲击B时,C将从超视图(B)中移除。

问题

触摸事件“通过”C到B。

我似乎无法找到如何忽略/阻止这些触摸事件;即,点击UILabel C不应该沿着响应者链传播。

例如,如果C是UIButtonUICollectionView等,它会自动“拦截”触摸事件。怎么会这样?它们都从UIResponder继承为UIView s ...

1 个答案:

答案 0 :(得分:3)

默认情况下,UILabel没有用户互动 - 所以它没有"拦截"触摸。

如果您在.isUserInteractionEnabled = true标签上设置了C,那么 " eat"接触。

修改:使用实际的集合视图单元格快速测试 ...您还需要在标签上添加手势识别器,以便#34; eat&#34 ;接触。在我的(新的,改进的)快速测试中对该标签进行了子类化:

class EatTouchesLabel: UILabel {

    var myGest: UIGestureRecognizer!

    required init(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)!
        self.commonInit()
    }

    override init(frame: CGRect) {
        super.init(frame: frame)
        self.commonInit()
    }

    func commonInit() {
        self.isUserInteractionEnabled = true
        if myGest == nil {
            myGest = UITapGestureRecognizer()
            self.addGestureRecognizer(myGest)
        }
    }

}