如何使视图的大小产生动画效果,使得视图可以使用框架高度增长或缩小?我需要在两个已知尺寸之间进行过渡。
我不知道您确切需要什么,但这里有一个非常基本的例子,其中包含一个Rectangle
,当您点击Button
时,它会被缩放:
我不确定您需要的内容,但这里有一个非常基本的示例,其中包含一个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)
}
}
}
请在您的下一个问题中添加一些代码或编辑该问题,以便人们可以提供更具体的答案。withAnimation { }
会在闭包内部动画化与状态变化相关的所有内容,因此我们可以使用 .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, value: animate) //you can change the animation you need
}
}
}