我有3个UIBezierPath,有2个圆,还有一条从1个圆的中心到另一个圆的线,看起来就像底部的图片。我想像上面的图片一样隐藏圆圈内的那部分线条。有什么简单的方法可以做到这一点吗?
我的策略是从中心画一条看不见的线,然后从两个圆的圆周上画一条黑线,因为我知道坡度等,但这似乎太多的工作。

private func pathForBoxCircle1() -> UIBezierPath {
let circlePath = UIBezierPath(arcCenter:circle1BoxCurrentCenter, radius: 25, startAngle: 0.0, endAngle: CGFloat(2*M_PI), clockwise: false)
//circlePath.fill()
pathBoxCircle1Global = circlePath
return circlePath
}
private func pathForBoxCircle2() -> UIBezierPath {
let circlePath = UIBezierPath(arcCenter:circle2BoxCurrentCenter, radius: 25, startAngle: 0.0, endAngle: CGFloat(2*M_PI), clockwise: false)
//circlePath.fill()
pathBoxCircle2Global = circlePath
return circlePath
}
private func pathForHorizonLine() -> UIBezierPath {
let path = UIBezierPath()
path.move(to: circle1BoxCurrentCenter)
path.addLine(to: circle2BoxCurrentCenter)
path.lineWidth = 5.0
//pathHorizonLineGlobal = path
return path
}
override func draw(_ rect: CGRect) {
pathForBoxCircle1().stroke()
pathForBoxCircle2().stroke() // same as stroke()
pathForHorizonLine().stroke()
}发布于 2017-08-05 09:10:21
不能在同一形状中混合透明和不透明线条。你需要画两个圆,然后画出从第一个圆的外部到第二个圆的外部的线段。
要做到这一点,你需要trig,或者可能是毕达哥拉斯,来计算连接线与两个圆相交的点的坐标。
如果C1是你的第一个圆,C2是你的第二个圆,C1在(C1.x,C1.y),C2在(C2.x,C2.y),C1的半径是R1,C2的半径是R2,那么伪代码将如下所示:
angle1 = atan2(C1.y - C2y, C1.x - C2.x)
angle2 = atan2(C2.y - C1.y, C2.x - C1.x)
xOffset1 = R1 * cos(angle1)
yOffset1 = R1 * sin(angle1)
point1 = (C1.x + xOffset1, C1.y + yOffset1)
xOffset2 = R2 * cos(angle2)
yOffset2 = R2 * sin(angle2)
point2 = (C2.x + xOffset2, C2.y + yOffset2)画出你的圆,然后在point1和point2之间画直线。
(请注意,我的trig有点生锈了,我是在一张草稿纸上画出来的。我认为它是正确的,但它完全未经测试。)
https://stackoverflow.com/questions/45516694
复制相似问题