struct MyClippedView: View {
var isCircular: Bool
var body: some View {
ClippedView()
// โ
OK (ๅฐ Circle ่ Capsule ๅ็บ AnyShape)
.clipShape(isCircular ? AnyShape(Circle()) : AnyShape(Capsule()))
ErrorView()
// ๐ mismatching types (Circle, Capsule ็บไธๅ้กๅ)
.clipShape(isCircular ? Circle() : Capsule())
}
}
ๅฎๆนๆ AnyShape ้ๅๅๅฅ๏ผไผฐ่จๅฏฆไฝๆนๅผๅฏ่ฝ้กไผผไธ้ข๏ผ
import SwiftUI
struct MyAnyShape: Shape {
private var wrappedShape: any Shape
init<S: Shape>(_ wrapped: S) {
self.wrappedShape = wrapped // โญ๏ธ ็ดๆฅๅฒๅญ Shape ๅฏฆไพ
}
// โญ๏ธ Shape requirement
func path(in rect: CGRect) -> Path {
wrappedShape.path(in: rect)
}
}
๐ ๅ่๏ผ has non-Sendable type
๐ ๆ็จ๏ผ view.clipShape()