【问题标题】:Why is my view not transitioning when first appeared on screen in swiftUI为什么我的视图在 swiftUI 中首次出现在屏幕上时没有转换
【发布时间】:2020-07-04 10:29:03
【问题描述】:

我想要一个在应用首次加载时从 0 缩放到 1 的简单视图。它怎么没有发生。看看我的代码:

struct ContentView: View {
    
    @State private var isLoadng = false
    
    var body: some View {
        
        ZStack {
            if isLoadng {
                Color.red
                    .cornerRadius(20)
                    .frame(width: 150, height: 200)
                    .transition(.scale)
               
            }
            
            
        }
        
        .onAppear {
            withAnimation(.easeInOut(duration: 2)) {
                self.isLoadng = true
            }
        }
    }
}

视图只是弹出,没有任何过渡

【问题讨论】:

    标签: swift animation swiftui transition


    【解决方案1】:

    这里是修改的部分。使用 Xcode 12 测试

    var body: some View {
        ZStack {
            if isLoadng {
                Color.red
                    .cornerRadius(20)
                    .frame(width: 150, height: 200)
                    .transition(.scale)
            }
        }
        .animation(.easeInOut(duration: 2))
        .onAppear {
            self.isLoadng = true
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-07
      • 1970-01-01
      • 1970-01-01
      • 2022-08-18
      • 1970-01-01
      • 2015-09-23
      相关资源
      最近更新 更多