未调用 collectionView didSelectItemAt indexPath

collectionView didSelectItemAt indexPath not being called

尝试 select 我的集合视图中的单元格时,没有任何反应。我已经设置了代理,但当用户点击时它不会被调用。

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    print("cell selected")
}

该单元格是包含 CAShapeLayer 和 UILabel 的自定义单元格。我试图乱用 isUserInteractionEnabled 方法但无济于事。

此外,如果我点击得非常快,有时会打印 "cell selected" 消息,但这种情况很少见。如果您有任何建议,请告诉我!

func drawCircle() {
    var circleLayer: CAShapeLayer!

    let arcCenter = CGPoint(x: frame.size.width / 2.0, y: (frame.size.height / 2.0) - 15)
    let circlePath = UIBezierPath(arcCenter: arcCenter, radius: (frame.size.width - 20) / 2, startAngle: 0.0, endAngle: CGFloat(.pi * 2.0), clockwise: true)

    circleLayer = CAShapeLayer()
    circleLayer.path = circlePath.cgPath
    circleLayer.fillColor = UIColor.clear.cgColor
    circleLayer.strokeColor = UIColor(named: "Fourth")?.cgColor
    circleLayer.lineWidth = 8.0

    circleLayer.strokeEnd = 1.0

    // Add the circleLayer to the view's layer's sublayers
    layer.addSublayer(circleLayer)
}

这是通过在我拥有的 UITapGestureRecognizer 上将 cancelsTouchesInView 方法设置为 false 来解决的。完整代码如下。

let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(self.hideKeyboardByTappingOutside))
    tap.cancelsTouchesInView = false
    self.view.addGestureRecognizer(tap)