如何将@namespace传递到SwiftUI中的多个视图? [英] How to pass @namespace to multiple Views in SwiftUI?

查看:291
本文介绍了如何将@namespace传递到SwiftUI中的多个视图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用新的Xcode 12 beta& SwiftUi 2.0. .matchedGeometryEffect()修饰符非常适合制作Hero动画. SwiftUI中引入了一个新属性@Namespace.它超酷.工作很棒.

I'm playing with the new Xcode 12 beta & SwiftUi 2.0. .matchedGeometryEffect() modifier is great to do Hero animations. There is a new property @Namespace is introduced in SwiftUI. Its super cool. working awesome.

我只是想知道是否有可能将命名空间变量传递给多个视图?

I was just wondering if there is any possibility to pass a Namespace variable to multiple Views?

这是我正在研究的示例,

Here is an example I'm working on,

struct HomeView: View {
    @Namespace var namespace
    @State var isDisplay = true
    
    var body: some View {
        ZStack {
            if isDisplay {
                VStack {
                    Image("share sheet")
                        .resizable()
                        .frame(width: 150, height: 100)
                        .matchedGeometryEffect(id: "img", in: namespace)
                    Spacer()
                }
                .frame(maxWidth: .infinity, maxHeight: .infinity)
                .background(Color.blue)
                .onTapGesture {
                    withAnimation {
                        self.isDisplay.toggle()
                    }
                }
            } else {
                VStack {
                    Spacer()
                    Image("share sheet")
                        .resizable()
                        .frame(width: 300, height: 200)
                        .matchedGeometryEffect(id: "img", in: namespace)
                }
                .frame(maxWidth: .infinity, maxHeight: .infinity)
                .background(Color.red)
                .onTapGesture {
                    withAnimation {
                        self.isDisplay.toggle()
                    }
                }
            }
        }
    }
}

一切正常.

但是,如果我想将Vstack提取为子视图,则下图显示我已将第一个VStack提取到了子视图中.

But if I want to extract the Vstack as a SubView, Below picture shows that I have extracted the first VStack into a subview.

我得到了称赞 Cannot find 'namespace' in scope

I'm getting a compliment Cannot find 'namespace' in scope

是否可以在多个视图之间传递名称空间?

Is there a way to pass namespace across multiple Views?

推荐答案

@NamaspaceNamaspace.ID的包装,您可以将Namaspace.ID的参数传递给子视图.

The @Namaspace is a wrapper for Namaspace.ID, and you can pass Namaspace.ID in argument to subviews.

这里是可能解决方案的演示.经过Xcode 12/iOS 14的测试

Here is a demo of possible solution. Tested with Xcode 12 / iOS 14

struct HomeView: View {
    @Namespace var namespace
    @State var isDisplay = true

    var body: some View {
        ZStack {
            if isDisplay {
                View1(namespace: namespace, isDisplay: $isDisplay)
            } else {
                View2(namespace: namespace, isDisplay: $isDisplay)
            }
        }
    }
}

struct View1: View {
    let namespace: Namespace.ID
    @Binding var isDisplay: Bool
    var body: some View {
        VStack {
            Image("plant")
                .resizable()
                .frame(width: 150, height: 100)
                .matchedGeometryEffect(id: "img", in: namespace)
            Spacer()
        }
        .frame(maxWidth: .infinity, maxHeight: .infinity)
        .background(Color.blue)
        .onTapGesture {
            withAnimation {
                self.isDisplay.toggle()
            }
        }
    }
}

struct View2: View {
    let namespace: Namespace.ID
    @Binding var isDisplay: Bool
    var body: some View {
        VStack {
            Spacer()
            Image("plant")
                .resizable()
                .frame(width: 300, height: 200)
                .matchedGeometryEffect(id: "img", in: namespace)
        }
        .frame(maxWidth: .infinity, maxHeight: .infinity)
        .background(Color.red)
        .onTapGesture {
            withAnimation {
                self.isDisplay.toggle()
            }
        }
    }
}

这篇关于如何将@namespace传递到SwiftUI中的多个视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆