SwiftUI - 动画调整视图框架的大小

Yar*_*arm 6 size animation view swiftui

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

Yod*_*ama 8

由于 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 
            }
        }
    }
Run Code Online (Sandbox Code Playgroud)


krj*_*rjw 7

我不知道你到底需要什么,但这里有一个非常基本的例子,Rectangle当你点击时它会被缩放Button

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)
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

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