1

我开始使用自定义容器,但我一直在尝试发送 @State var。示例代码:

struct ZoomCardView<Content: View>: View {
    let contentDefault: ((Binding<Bool>)) -> Content
    let contentZoomed: (Binding<Bool>) -> Content
    
    init(@ViewBuilder defaultContent: @escaping (Binding<Bool>) -> Content, @ViewBuilder zoomed: @escaping (Binding<Bool>) -> Content) {
        self.contentDefault = defaultContent
        self.contentZoomed = zoomed
    }
    
    @State private var isShowing: Bool = false
    
    var body: some View {
        HStack {
            if isShowing {
                self.contentZoomed($isShowing)
                    .transition(.scale(scale: 0, anchor: .center))
            } else {
                self.contentDefault($isShowing)
                    .transition(.scale(scale: 0, anchor: .center))
            }
        }
    }
}

这个想法是调用站点将呈现内容并提供切换isShowingvar 的机制。基本原理是允许调用站点决定哪个手势或 UI 元素控制切换:

struct ContentView: View {
    var body: some View {
        ZoomCardView { isShowing in
            Button(action: {
                isShowing.toggle()  <---- *** Error (see below)
            }, label: {
                Text("Open")
            })
        } zoomed: { isShowing in
            Button(action: {
                isShowing.toggle()  <---- *** Error (see below)
            }, label: {
                Text("Zoomed")
            })
        }
        .frame(width: 300, height: 300)
        .cornerRadius(15)
        .shadow(radius: 5)
    }
}

问题:我Cannot call value of non-function type 'Binding<() -> ()>在上面的两isShowing.toggle()行中得到一个。我不确定为什么。有任何想法吗?

4

0 回答 0