如何快速设置CAShapeLayer的strokeColor

时间:2020-02-24 05:20:31

标签: ios swift calayer uibezierpath cashapelayer

我想绘制对角线视图。

这是我的代码。

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

为什么会这样?

1 个答案:

答案 0 :(得分:1)

这是代码段:

        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)

结果是:

enter image description here