Last updated 2 months ago
Was this helpful?
SwiftUI โฉ shapes โฉ Shape โฉ AnyShape
A type-erased shape value. You can use this type to dynamically switch between shape types.
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()
SwiftUI โฉ Shapes โฉ AnyShape
type erasure
has non-Sendable type
mismatching types