Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/115.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 使用获取帧的动态位置,在两个UIButton之间绘制一条虚线_Ios_Swift_Core Graphics - Fatal编程技术网

Ios 使用获取帧的动态位置,在两个UIButton之间绘制一条虚线

Ios 使用获取帧的动态位置,在两个UIButton之间绘制一条虚线,ios,swift,core-graphics,Ios,Swift,Core Graphics,嗨,我想画一条虚线从一个按钮到另一个按钮 我用这段代码在以下几点之间画了一条线: func addDashedLine(fromPoint start: CGPoint, toPoint end:CGPoint) { let line = CAShapeLayer() let linePath = UIBezierPath() linePath.moveToPoint(start) linePath.addLineToPoint(end) line.pat

嗨,我想画一条虚线从一个按钮到另一个按钮

我用这段代码在以下几点之间画了一条线:

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)
}
要获取起点和终点,我将使用button.center作为两个按钮。但这条线不是从中心到中心画的。它在UIButton的外面画。我已经为按钮使用了Autolayout,并使用乘数从superview设置了约束

请告诉我怎么做

我的建议:

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,则这可能不起作用

您的按钮是在self.view中还是在其他包含子视图中?在这种情况下,它们的中心点表示它们在这些子视图中的位置。您可能希望将该点转换为如下主视图坐标:self.view.convertPointbutton.center,fromView:buttonContainerView