如何在 swift 中设置 CAShapeLayer 的 strokeColor

How to set CAShapeLayer 's strokeColor in swift

我想绘制对角视图。

这是我的代码。

override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)

    let path = UIBezierPath()
    path.move(to: CGPoint.zero)
    path.addLine(to: CGPoint(x: 50, y: 50))

    let view = UIView(frame: CGRect(x: 100, y: 100, width: 50, height: 50))
    view.backgroundColor = UIColor.blue

    let mask = CAShapeLayer()
    mask.frame = view.bounds
    mask.path = path.cgPath
    mask.strokeColor = UIColor.orange.cgColor
    mask.lineWidth = 25

    view.layer.mask = mask

    self.view.addSubview(view)
}

我将 strokeColor 设置为 orange。但是颜色是蓝色backgroundColor

为什么会这样?

这是代码片段:

        let path = UIBezierPath()
        path.move(to: CGPoint.zero)
        path.addLine(to: CGPoint(x: 50, y: 50))

        let view = UIView(frame: CGRect(x: 100, y: 100, width: 50, height: 50))
        view.backgroundColor = UIColor.green

        let diagonal = CAShapeLayer()
        diagonal.frame = view.bounds
        diagonal.path = path.cgPath
        diagonal.strokeColor = UIColor.orange.cgColor
        diagonal.lineWidth = 15
        diagonal.backgroundColor = UIColor.blue.cgColor
        // view.layer.mask = mask

        view.layer.addSublayer(diagonal)

        self.view.addSubview(view)

这导致: