在SwiftUI中禁用设备旋转动画的最佳方法是使用onRotate方法,并将withAnimation参数设置为nil。
Best way to disable SwiftUI orientation change animation在SwiftUI中禁用设备旋转动画的最佳方法是使用onRotate方法,并将withAnimation参数设置为nil。
Best way to disable SwiftUI orientation change animationstruct 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 !!
}