在使用BezierPath clipShape时,如果遇到无法剪裁成功的问题,可以尝试在clipShape前加上一个GeometryReader,并将BezierPath包裹在一个Path形式的View中,如下所示:
struct ClipPathView: View {
var body: some View {
GeometryReader { geo in
Path { path in
path.move(to: CGPoint(x: 0, y: 0))
path.addLine(to: CGPoint(x: geo.size.width, y: 0))
path.addLine(to: CGPoint(x: geo.size.width, y: geo.size.height))
path.addLine(to: CGPoint(x: 0, y: geo.size.height))
path.closeSubpath()
}.fill(Color.blue)
.clipShape(
BezierPath { path in
path.move(to: CGPoint(x: 50, y: 50))
path.addLine(to: CGPoint(x: 150, y: 50))
path.addLine(to: CGPoint(x: 150, y: 150))
path.addLine(to: CGPoint(x: 50, y: 150))
path.closeSubpath()
}
)
}
}
}
通过将BezierPath包裹在一个Path形式的View中,并将其与GeometryReader一起使用,可以保证正确剪切路径。