gpt4 book ai didi

ios - CAShapeLayer 线条的圆角

转载 作者:塔克拉玛干 更新时间:2023-11-02 09:56:01 25 4
gpt4 key购买 nike

我想在 View 中绘制带圆角的对角线。我的代码:

class DiagonalView: UIView {

// MARK: - Public Properties

@IBInspectable var fillColor: UIColor = UIColor.white {
didSet {
self.diagonalLayer.strokeColor = fillColor.cgColor
self.redraw()
}
}

@IBInspectable var lineWidth: CGFloat = 1 {
didSet {
self.diagonalLayer.lineWidth = lineWidth
self.redraw()
}
}

// MARK: - Private Properties

private var diagonalLayer: CAShapeLayer = {
let layer = CAShapeLayer()
layer.fillColor = UIColor.clear.cgColor
layer.strokeColor = UIColor.white.cgColor
layer.lineCap = kCALineCapRound
layer.lineWidth = 1
return layer
}()

// MARK: - Constructors

override func awakeFromNib() {
super.awakeFromNib()
self.layer.addSublayer(self.diagonalLayer)
self.setupPath()
}

override func layoutSubviews() {
super.layoutSubviews()
self.setupPath()
}

// MARK: - Private methods

private func redraw() {
self.setNeedsDisplay()
}

private func setupPath() {
let startPoint = CGPoint(x: self.frame.width, y: 0)
let endPoint = CGPoint(x: 0, y: self.frame.height)
let path = UIBezierPath()
path.move(to: startPoint)
path.addLine(to: endPoint)
path.close()
self.diagonalLayer.path = path.cgPath
}

}

行位正确。但是角不是圆的。结果:

enter image description here

代码看起来不错,但我觉得我搞砸了……有什么建议吗?

最佳答案

我想我找到了问题所在。文档说:

线帽样式指定了描边时开放路径端点的形状。

你正在关闭你的路径。保持打开状态。

关于ios - CAShapeLayer 线条的圆角,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39915285/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com