我有一个广场,位于分门座的滚动观点内。 当对广场进行点击时,我想将其与地表层的顶层联系起来,并将其置于其他一切之外,使用相应的地表效应。 我不能 get。
Problem I present the square at the top of the view hierarchy through a window which makes the animation fail. How can I correctly animate with a window?
struct SomeView: View {
@EnvironmentObject var popRoot: PopToRoot
@Namespace private var animation
var body: some View {
ScrollView {
RoundedRectangle(cornerRadius: 10)
.matchedGeometryEffect(id: "squareAnim", in: animation)
.foregroundStyle(.blue).frame(width: 50, height: 50)
.onTapGesture {
withAnimation {
popRoot.showOverlay = true
}
}
//other sub views
}
}
}
struct ContentView: View { //top of view hierarchy
@EnvironmentObject var popRoot: PopToRoot
@Namespace private var animation
var body: some View {
SomeView()
.overlay {
if showOverlay {
TopViewWindow()
.matchedGeometryEffect(id: "squareAnim", in: animation)
// ----- HERE
}
}
}
}
class PopToRoot: ObservableObject { //envirnment object to be accessed anywhere
@Published var showOverlay = false
}
超越一切内容的视觉角色
video player that goes above everything
struct TopView: View {
var body: some View {
ZStack {
RoundedRectangle(cornerRadius: 10)
.foregroundStyle(.blue).frame(width: 100, height: 100)
//other views
}.ignoresSafeArea()
}
}
struct TopViewWindow: View {
@State private var hostingController: UIHostingController<TopView>? = nil
func showImage() {
let swiftUIView = TopView()
hostingController = UIHostingController(rootView: swiftUIView)
hostingController?.view.backgroundColor = .clear
hostingController?.view.frame = CGRect(
x: 0,
y: 0,
width: UIScreen.main.bounds.width,
height: UIScreen.main.bounds.height)
if let windowScene = UIApplication.shared.connectedScenes.first as? UIWindowScene,
let window = windowScene.windows.first {
// withAnimation { doesnt work here either
window.addSubview(hostingController!.view)
hostingController?.view.center.x = window.center.x
}
}
func dismissImage() {
hostingController?.view.removeFromSuperview()
hostingController = nil
}
var body: some View {
VStack {}.onAppear { showImage() }.onDisappear { dismissImage() }
}
}