gpt4 book ai didi

swift - 带圆角的虚线

转载 作者:搜寻专家 更新时间:2023-10-30 21:50:10 24 4
gpt4 key购买 nike

<分区>

我正在尝试在这样的 View 周围绘制带圆角的虚线:

class DashedLineView: UIView {

override func draw(_ rect: CGRect) {

let path = UIBezierPath(roundedRect: rect, cornerRadius: 8)

UIColor.clear.setFill()
path.fill()

UIColor.red.setStroke()
path.lineWidth = 3

let dashPattern : [CGFloat] = [3, 3]
path.setLineDash(dashPattern, count: 2, phase: 0)
path.stroke()
}
}

结果是:

enter image description here

如您所见,边角存在问题,知道如何解决吗?

更新:使用@Jon Rose answer DashedLineView 现在看起来像这样:

class DashedLineView: UIView {

private let borderLayer = CAShapeLayer()

override func awakeFromNib() {

super.awakeFromNib()

borderLayer.strokeColor = UIColor.red.cgColor
borderLayer.lineDashPattern = [3,3]
borderLayer.backgroundColor = UIColor.clear.cgColor
borderLayer.fillColor = UIColor.clear.cgColor

layer.addSublayer(borderLayer)
}

override func draw(_ rect: CGRect) {

borderLayer.path = UIBezierPath(roundedRect: rect, cornerRadius: 8).cgPath
}
}

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