好吧所以我试图在视图中绘制一条虚线,这应该相当简单,但无论我尝试过什么,我都会在两端剪辑时留下破折号,我想让破折号没有有任何剪裁的段。这是我做的扩展(紫色背景颜色就在那里,所以我可以看到框架)
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
}
不知道我在哪里乱搞,但我知道我必须做错事,因为在swift中画一条虚线应该不是很难(我希望)。
答案 0 :(得分:0)
您可以使用lineDashPhase
调整模式的开始位置。尝试
shapeLayer.lineDashPhase = -4