Эффект согласованной геометрии не работает с окном ⇐ IOS
Эффект согласованной геометрии не работает с окном
I have a square that is embedded within a scroll view inside a subview. When the square is clicked I want to animate it to the top of the view hierarchy and present it above everything else using a matched geometry effect. I cannot get the animation to work.
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? = 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() } } }
Источник: https://stackoverflow.com/questions/780 ... ith-window
I have a square that is embedded within a scroll view inside a subview. When the square is clicked I want to animate it to the top of the view hierarchy and present it above everything else using a matched geometry effect. I cannot get the animation to work.
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? = 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() } } }
Источник: https://stackoverflow.com/questions/780 ... ith-window
-
- Похожие темы
- Ответы
- Просмотры
- Последнее сообщение
-
-
Нежелательный эффект кроссфейда в эффекте согласованной геометрии SwiftUI
Anonymous » » в форуме IOS - 0 Ответы
- 39 Просмотры
-
Последнее сообщение Anonymous
-