如何将 @State 绑定传递给自定义容器的 Content 闭包?

问题描述

我开始使用自定义容器,但一直在尝试发送 @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))
            }
        }
    }
}

这个想法是调用站点将呈现内容并提供切换 isShowing 变量的机制。其基本原理是允许调用站点决定哪个手势或 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()。我不知道为什么。有什么想法吗?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...