SwiftUI如何移除fullScreenCover全屏弹窗的过渡动画效果
移除SwiftUI fullScreenCover动画的可行方案
- 方案1:通过Transaction直接禁用弹窗绑定的动画(适配你当前的代码场景,修改成本最低)
直接在你的fullScreenCover挂载视图上追加transaction配置即可,修改后的代码如下:
struct ContentView: View { var body: some View { EmptyView() .fullScreenCover(isPresented: .constant(true), content: { FullScreenView.init() }) .transaction { transaction in transaction.disablesAnimations = true } } }
该方案对弹出、消失的过渡动画都会生效,不需要修改其他业务逻辑。
- 方案2:触发弹窗时临时禁用全局动画
如果你的弹窗是通过状态变量控制显隐,在变更状态的逻辑外层包裹withAnimation(.none)即可:
struct ContentView: View { @State private var isShowCover = false var body: some View { Button("弹出全屏窗") { withAnimation(.none) { isShowCover = true } } .fullScreenCover(isPresented: $isShowCover, content: { FullScreenView.init() }) } }
- 方案3:UIKit混编场景下调用原生present接口
你当前项目是UIKit+SwiftUI混编架构,也可以跳过SwiftUI的fullScreenCover接口,直接用UIKit的present方法实现无动画弹出:
// 在你的UIViewController中调用 let fullScreenView = UIHostingController(rootView: FullScreenView()) fullScreenView.modalPresentationStyle = .fullScreen // animated参数设为false即可禁用动画 present(fullScreenView, animated: false)
注意:以上方案只会禁用fullScreenCover自带的系统级过渡动画,不会影响FullScreenView内部自定义的视图动画效果
内容的提问来源于stack exchange,提问作者MunichCoder
相关产品推荐
相关产品推荐

