【问题标题】:CAShapeLayer dashed line clipping at end of view视图末端的 CAShapeLayer 虚线剪裁
【发布时间】:2017-03-31 05:07:59
【问题描述】:

好的,所以我正在尝试在视图内绘制一条虚线,这应该相当简单,但无论我尝试过什么,我都会在两端剪裁破折号,我不希望破折号有任何剪辑片段。这是我做的扩展(紫色背景颜色就在那里,所以我可以看到框架)

extension UIView {
public func addDashedLineToView(_ color : UIColor) {
    self.backgroundColor = UIColor.purple
    let cgColor = color.cgColor

    let shapeLayer: CAShapeLayer = CAShapeLayer()
    let frameSize = self.frame.size
    let shapeRect = CGRect(x: 0, y: 0, width: frameSize.width, height: frameSize.height)

    shapeLayer.bounds = shapeRect
    shapeLayer.position = CGPoint(x: frameSize.width / 2, y: frameSize.height)
    shapeLayer.fillColor = UIColor.clear.cgColor
    shapeLayer.strokeColor = cgColor
    shapeLayer.lineWidth = 6
    shapeLayer.lineCap = kCALineCapRound
    shapeLayer.lineJoin = kCALineJoinRound
    shapeLayer.lineDashPattern = [8, 10]

    let path: CGMutablePath = CGMutablePath()
    path.move(to: .zero)
    path.addLine(to: CGPoint(x: self.frame.width, y: 0))
    shapeLayer.path = path

    self.layer.addSublayer(shapeLayer)
    self.layer.masksToBounds = true
}

这是它最终的样子的屏幕截图。

不确定我在哪里搞砸了,但我知道我一定做错了什么,因为快速画一条虚线不应该这么难(我希望如此)。

【问题讨论】:

    标签: swift3 cashapelayer


    【解决方案1】:

    您可以使用lineDashPhase 调整图案的起始位置。试试

    shapeLayer.lineDashPhase = -4
    

    【讨论】:

    • 谢谢,看来那是我遗漏的部分。
    猜你喜欢
    • 1970-01-01
    • 2013-06-05
    • 1970-01-01
    • 2012-06-07
    • 2016-05-04
    • 1970-01-01
    • 2015-08-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多