【问题标题】:SwiftUI - Animate resize of a view frameSwiftUI - 动画调整视图框架的大小
【发布时间】:2020-01-13 13:18:04
【问题描述】:

您如何为视图的大小设置动画,以便视图可以使用框架高度放大或缩小? 我需要在两个已知维度之间转换。

【问题讨论】:

  • 你是怎么尝试的?

标签: animation view size swiftui


【解决方案1】:

我不确切知道您需要什么,但这是一个非常基本的示例,当您点击 Button 时,Rectangle 会缩放:

struct ContentView: View {

    @State var animate = false

    var body: some View {
        VStack {
            Button(action: {
                withAnimation {
                    self.animate.toggle()
                }
            }, label: {
                Text("Animate")
            })
            Rectangle()
                .foregroundColor(.blue)
                .frame(width: self.animate ? 100 : 150, height: self.animate ? 60 : 90)
        }
    }
}

请在您的下一个问题中添加一些代码或编辑问题,以便人们提供更具体的答案。

【讨论】:

    【解决方案2】:

    由于 withAnimation { } 为闭包内与 State 变化相关的所有内容设置动画,我们可以使用 .animation() 修饰符为特定的内容设置动画。

    struct ContentView: View {
    
        @State var animate = false
    
        var body: some View {
            VStack {
                Button(action: {
                        self.animate.toggle()
                }, label: {
                    Text("Animate")
                })
                Rectangle()
                    .foregroundColor(.blue)
                    .frame(width: animate ? 100 : 150, height: animate ? 60 : 90)
                    .animation(.default) //you can change the animation you need 
                }
            }
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-02-20
      • 1970-01-01
      • 2015-06-16
      • 2015-01-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多