ios - 使用动态获取 Frame 位置在两个 UIButton 之间绘制虚线

标签 ios swift core-graphics

嗨,我想绘制从一个 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/

相关文章:

performance - 优化-webkit-transform 性能的技巧?

ios - Swift:根据提供的用户信息读取字符串

ios - 标签栏 Controller 内的导航 Controller 不显示标题

ios - 如何检查图像是否为零?

ios - 为什么当线本身加倍时连接不四舍五入?

objective-c - 如何使用类似 QuartzComposer 的 GUI 设计应用程序

ios - NSString 比较不适用于 iPAD

ios - 为什么不应该直接调用收据验证端点

ios - 将 Swift 的调用同步到基于 C 的线程不安全库

ios - 为什么 drawRect : work without calling [super drawrect:rect]?