如何快速设置CAShapeLayer的strokeColor

问题描述 投票:0回答:1

我想绘制对角线视图。

这是我的代码。

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

为什么会这样?

ios swift uibezierpath cashapelayer
1个回答
-1
投票
enter image description here
override func viewDidLoad() {
    super.viewDidLoad()

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

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

    let lineLayer = CAShapeLayer()
    lineLayer.path        = path.cgPath
    lineLayer.strokeColor = UIColor.orange.cgColor
    lineLayer.lineWidth   = 25
    viewMask.layer.addSublayer(lineLayer)

    self.view.addSubview(viewMask)

}
© www.soinside.com 2019 - 2024. All rights reserved.