Swift:创建路径时在两次 addArc 调用之间忽略 addLine
Swift: addLine ignored in between two addArc calls when creating a path
这段代码符合我的预期。它绘制一个圆弧,然后在该圆弧的顶部添加一条 50 点宽的线:
path.move(to: .init(x: myX, y: myY))
path.addArc(withCenter: CGPoint(x: centerX, y: centerY), radius: radius1, startAngle: .pi, endAngle: (3 * .pi)/2, clockwise: true)
let currentPoint = path.currentPoint
path.addLine(to: CGPoint(x: currentPoint.x + 50, y: currentPoint.y))
此代码忽略 addLine
添加 50 点宽的线,仅在第一条弧线的顶部开始第二条弧线。
path.move(to: .init(x: myX, y: myY))
path.addArc(withCenter: CGPoint(x: centerX, y: centerY), radius: radius1, startAngle: .pi, endAngle: (3 * .pi)/2, clockwise: true)
let currentPoint = path.currentPoint
path.addLine(to: CGPoint(x: currentPoint.x + 50, y: currentPoint.y))
path.addArc(withCenter: CGPoint(x: centerX + 50, y: centerY), radius: radius1, startAngle: (3 * .pi)/2, endAngle: .pi, clockwise: false)
对于第二段代码,如果我注释掉 addLine
代码,我会得到完全相同的输出。如果我更改 addLine 代码以添加 300 pixels 点而不是 50 个点,我会得到完全相同的输出。addLine 代码被忽略,我得到两条弧线,在第一端之间没有线第二个开始。
有什么建议吗?非常感谢!
你说:
With this second bit of code, I get the exact same output if I comment out the addLine code.
是的,当您向现有路径添加弧线时,它会自动绘制一条从 currentPoint
到第二条弧线起点的直线。如果你不希望它在中间添加线,你需要在你的路径中做一个 move(to:)
如果你不想要中间的线,那么第二个弧将开始的地方。或者创建两条路径,每条弧线一条,然后分别描边。
I get the exact same output if I change the addLine
code to add 300 pixels instead of 50.
这不太合理,我无法重现该行为。例如,这是我将第二个圆弧移动 50pt 时得到的结果(我将为笔画制作动画,以便您可以看到发生了什么):
但这是我将线移动 300pt 时得到的结果(但第二个弧距离第一个弧仅 50pt):
很明显,如果您不仅将线设为 300pt 长,而且将第二个圆弧的中心也移动 300pt,那么它将与第一个示例一样(除了更远)。
但是,如果我将你的 addLine(to:)
替换为 move(to:)
,那么你将不会得到它们之间的线:
FWIW,在所有这些示例中,我不知道您在 myX
和 myY
中使用什么,所以我使用了第一个圆弧左侧的点。显然,如果您不想要那条额外的线,请将 myX
和 myY
移动到第一条弧线的开头(或者只是将其完全注释掉)。
我不知道你的代码有什么问题,但我已经尝试过..它可能对你有帮助,所以发布它
@IBDesignable
class CustomBg:UIView {
private lazy var curvedLayer: CAShapeLayer = {
let shapeLayer = CAShapeLayer()
shapeLayer.fillColor = UIColor.yellow.cgColor
shapeLayer.lineWidth = 4
shapeLayer.strokeColor = UIColor.green.cgColor
return shapeLayer
}()
//MARK:- inializer
override init(frame: CGRect) {
super.init(frame: frame)
layer.addSublayer(curvedLayer)
}
required init?(coder: NSCoder) {
super.init(coder: coder)
layer.addSublayer(curvedLayer)
}
override func layoutSubviews() {
super.layoutSubviews()
updatePath()
}
func updatePath() {
let centerX = 100
let centerY = 100
let radius1 = CGFloat( 50.0)
let path = UIBezierPath()
path.addArc(withCenter: CGPoint(x: centerX, y: centerY), radius: radius1, startAngle:0, endAngle: (2 * .pi), clockwise: true)
let currentPoint = path.currentPoint
path.addLine(to: CGPoint(x: currentPoint.x + 150, y: currentPoint.y))
path.addArc(withCenter: CGPoint(x: Int(path.currentPoint.x + radius1) , y: centerY), radius: radius1 , startAngle: .pi, endAngle: (3 * .pi ), clockwise: true)
curvedLayer.path = path.cgPath
}
}
这段代码符合我的预期。它绘制一个圆弧,然后在该圆弧的顶部添加一条 50 点宽的线:
path.move(to: .init(x: myX, y: myY))
path.addArc(withCenter: CGPoint(x: centerX, y: centerY), radius: radius1, startAngle: .pi, endAngle: (3 * .pi)/2, clockwise: true)
let currentPoint = path.currentPoint
path.addLine(to: CGPoint(x: currentPoint.x + 50, y: currentPoint.y))
此代码忽略 addLine
添加 50 点宽的线,仅在第一条弧线的顶部开始第二条弧线。
path.move(to: .init(x: myX, y: myY))
path.addArc(withCenter: CGPoint(x: centerX, y: centerY), radius: radius1, startAngle: .pi, endAngle: (3 * .pi)/2, clockwise: true)
let currentPoint = path.currentPoint
path.addLine(to: CGPoint(x: currentPoint.x + 50, y: currentPoint.y))
path.addArc(withCenter: CGPoint(x: centerX + 50, y: centerY), radius: radius1, startAngle: (3 * .pi)/2, endAngle: .pi, clockwise: false)
对于第二段代码,如果我注释掉 addLine
代码,我会得到完全相同的输出。如果我更改 addLine 代码以添加 300 pixels 点而不是 50 个点,我会得到完全相同的输出。addLine 代码被忽略,我得到两条弧线,在第一端之间没有线第二个开始。
有什么建议吗?非常感谢!
你说:
With this second bit of code, I get the exact same output if I comment out the addLine code.
是的,当您向现有路径添加弧线时,它会自动绘制一条从 currentPoint
到第二条弧线起点的直线。如果你不希望它在中间添加线,你需要在你的路径中做一个 move(to:)
如果你不想要中间的线,那么第二个弧将开始的地方。或者创建两条路径,每条弧线一条,然后分别描边。
I get the exact same output if I change the
addLine
code to add 300 pixels instead of 50.
这不太合理,我无法重现该行为。例如,这是我将第二个圆弧移动 50pt 时得到的结果(我将为笔画制作动画,以便您可以看到发生了什么):
但这是我将线移动 300pt 时得到的结果(但第二个弧距离第一个弧仅 50pt):
很明显,如果您不仅将线设为 300pt 长,而且将第二个圆弧的中心也移动 300pt,那么它将与第一个示例一样(除了更远)。
但是,如果我将你的 addLine(to:)
替换为 move(to:)
,那么你将不会得到它们之间的线:
FWIW,在所有这些示例中,我不知道您在 myX
和 myY
中使用什么,所以我使用了第一个圆弧左侧的点。显然,如果您不想要那条额外的线,请将 myX
和 myY
移动到第一条弧线的开头(或者只是将其完全注释掉)。
我不知道你的代码有什么问题,但我已经尝试过..它可能对你有帮助,所以发布它
@IBDesignable
class CustomBg:UIView {
private lazy var curvedLayer: CAShapeLayer = {
let shapeLayer = CAShapeLayer()
shapeLayer.fillColor = UIColor.yellow.cgColor
shapeLayer.lineWidth = 4
shapeLayer.strokeColor = UIColor.green.cgColor
return shapeLayer
}()
//MARK:- inializer
override init(frame: CGRect) {
super.init(frame: frame)
layer.addSublayer(curvedLayer)
}
required init?(coder: NSCoder) {
super.init(coder: coder)
layer.addSublayer(curvedLayer)
}
override func layoutSubviews() {
super.layoutSubviews()
updatePath()
}
func updatePath() {
let centerX = 100
let centerY = 100
let radius1 = CGFloat( 50.0)
let path = UIBezierPath()
path.addArc(withCenter: CGPoint(x: centerX, y: centerY), radius: radius1, startAngle:0, endAngle: (2 * .pi), clockwise: true)
let currentPoint = path.currentPoint
path.addLine(to: CGPoint(x: currentPoint.x + 150, y: currentPoint.y))
path.addArc(withCenter: CGPoint(x: Int(path.currentPoint.x + radius1) , y: centerY), radius: radius1 , startAngle: .pi, endAngle: (3 * .pi ), clockwise: true)
curvedLayer.path = path.cgPath
}
}