我想在 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
}
}
行位置正确。但角不是圆的。 结果:
代码看起来不错,但我觉得我搞砸了……有什么建议吗?
我想我找到了问题所在。文档说:
线帽样式指定描边时开放路径的端点形状。
你正在关闭你的路径。保持打开状态。
关于ios - CAShapeLayer 线的圆角,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39915285/
欢迎光临 OStack程序员社区-中国程序员成长平台 (https://ostack.cn/) | Powered by Discuz! X3.4 |