我正在用Path绘制一条线,在那条线上我想有一个圆,如果我在周围玩耍,我当然可以在该线上画圆。但是我不明白为什么这段代码没有把圆圈圈起来:
struct CircleOnLineView: View {
func createCirle() -> some View {
return Circle().fill(Color.blue)
}
var body: some View {
GeometryReader { geometry in
ZStack {
Path { path in
path.move(to: CGPoint(x: 0, y: geometry.size.height / 2))
path.addLine(to: CGPoint(x: geometry.size.width, y: geometry.size.height / 2))
}
.stroke(Color.gray, lineWidth: 3)
Circle()
.fill(Color.blue)
.position(CGPoint(x: 0 , y: geometry.size.height / 2))
.frame(width: 5, height: 5)
}
}
}
}
在这种情况下,修饰符的顺序很重要。这是预期的方式(第一个-制成形状的尺寸,第二个-放置形状的尺寸):
<< img src =“ https://image.soinside.com/eyJ1cmwiOiAiaHR0cHM6Ly9pLnN0YWNrLmltZ3VyLmNvbS94NFJkMC5wbmcifQ==” alt =“在此处输入图像描述”>
Circle()
.fill(Color.blue)
.frame(width: 5, height: 5)
.position(CGPoint(x: 0 , y: geometry.size.height / 2))