代码之家  ›  专栏  ›  技术社区  ›  Mr. Man

收银员不接球

  •  0
  • Mr. Man  · 技术社区  · 7 年前

    我在一个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的in it中将它添加到视图中,如下所示:

        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")
        }
    }
    

    但当我触摸到塑形器内部时,什么也没印出来。我做错什么了?

    0 回复  |  直到 7 年前
    推荐文章