CAShapeLayer未收到任何触摸

时间:2018-06-25 00:56:56

标签: swift uiview calayer cashapelayer cgpath

我在UIView内部制作了一个CAShapeLayer,就像这样:

    let shapeMask: CAShapeLayer = {
        let sl = CAShapeLayer()
        sl.bounds = CGRect(x: 0, y: 0, width: 133, height: 133)
        sl.lineWidth = 2
        sl.strokeColor = UIColor.green.cgColor
        sl.fillColor = UIColor.purple.cgColor
        let shapeLayerPath = UIBezierPath()
        shapeLayerPath.addArc(withCenter: CGPoint(x: 0, y: 0), radius: 68, startAngle: -.pi/2, endAngle: -.pi/4, clockwise: true)
        shapeLayerPath.addArc(withCenter: CGPoint(x: 0, y: 0), radius: 180, startAngle: -.pi/4, endAngle: -.pi/2, clockwise: false)
        shapeLayerPath.move(to: CGPoint(x: 0, y: -68))
        shapeLayerPath.addLine(to: CGPoint(x: 0, y: -180))
        shapeLayerPath.close()
        sl.path = shapeLayerPath.cgPath            
        return sl
    }()

我将其添加到UIView的init的视图中,如下所示:

    layer.addSublayer(shapeMask)
    shapeMask.frame = CGRect(x: 181, y: 180, width: 133, height: 133)

看起来像这样:

Shape Layer

我正在使用touchesBegan来查看用户是否触摸ShapeLayer内部

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {        
    let touchOne = touches.first
    let point = touchOne!.location(in: self)

    if (shapeMaskOne.path?.contains(point))! {
        print("shapeMaskTouched")
    }
}

但是当我触摸ShapeLayer内部时,什么也不会打印。我在做什么错了?

0 个答案:

没有答案