如何在SwiftUI中禁用设备旋转动画?

10

你找到解决这个问题的方法了吗?我也遇到了同样的问题。 - Andrei Matei
顶一下。这方面有什么消息吗?一直在寻找解决方案。 - Sylar
1个回答

1
我们可以以几乎相同的方式实现这一点 - 创建一个带有动画阻止器的辅助视图控制器,并将其用作可表示的背景。
已在Xcode 13.4 / iOS 15.5上测试通过。
struct HelperView: UIViewControllerRepresentable {
    func makeUIViewController(context: Context) -> some UIViewController {
        OrientationHandler()
    }

    func updateUIViewController(_ uiViewController: UIViewControllerType, context: Context) {
    }

    class OrientationHandler: UIViewController {
        override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
            coordinator.animate(alongsideTransition: nil) { _ in
                UIView.setAnimationsEnabled(true)
            }
            UIView.setAnimationsEnabled(false)
            super.viewWillTransition(to: size, with: coordinator);
        }
    }
}

使用方法:

    WindowGroup {
        ContentView()
          .background(HelperView())   // << here !!
    }

Module on GitHub


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接