Last updated 3 months ago
Was this helpful?
โฉ โฉ โฉ AnyShape
A 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()) } }
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) } }