嗨,我想绘制从一个 UIButton 到另一个按钮的虚线。
我用这段代码在点之间画线:
func addDashedLine(fromPoint start: CGPoint, toPoint end:CGPoint) {
let line = CAShapeLayer()
let linePath = UIBezierPath()
linePath.moveToPoint(start)
linePath.addLineToPoint(end)
line.path = linePath.CGPath
line.strokeColor = UIColor.redColor().CGColor
line.lineWidth = 1
//line.lineJoin = kCALineJoinRound
line.lineDashPattern = [8, 8]
self.view.layer.addSublayer(line)
}
为了获取 startPoint 和 Endpoint,我对两个按钮都使用了button.center()。但线并不是从中心到中心绘制的。它是在 UIButton 之外绘制的。我使用了按钮的自动布局,并使用乘数设置了 super View 的约束。
请建议我该怎么做。?
最佳答案
我的建议:
let startPoint = CGPoint(x: button1.frame.origin.x + button1.frame.size.width, y: button1.frame.origin.y + (button1.frame.size.height/2.0))
let endPoint = CGPoint(x: button2.frame.origin.x, y: button2.frame.origin.y + (button2.frame.size.height / 2.0))
addDashedLine(fromPoint start:stratrPoint, toPoint end:endPoint)
编辑
但也许如果您使用的是 AutoLayout,如果您使用的是 StackView,这应该不起作用
关于ios - 使用动态获取 Frame 位置在两个 UIButton 之间绘制虚线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43115579/