【发布时间】:2020-11-17 16:33:09
【问题描述】:
我正在使用新的 Xcode 12 beta 和 SwiftUi 2.0。 .matchedGeometryEffect() 修饰符非常适合制作英雄动画。 SwiftUI 中引入了一个新属性 @Namespace。它超级酷。工作很棒。
我只是想知道是否有可能将命名空间变量传递给多个视图?
这是我正在研究的一个例子,
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提取到子视图中。
我得到了表扬Cannot find 'namespace' in scope
有没有办法跨多个视图传递命名空间?
【问题讨论】: