CAShapeLayer系列的圆角

时间:2016-10-07 10:38:54

标签: ios swift3 cashapelayer

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

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

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

1 个答案:

答案 0 :(得分:3)

我想我发现了这个问题。文档说:

线条样式指定描边时打开路径的端点的形状。

你正在关闭你的道路。保持开放状态。