由于 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)
我不知道你到底需要什么,但这里有一个非常基本的例子,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)
请在您的下一个问题中添加一些代码或编辑问题,以便人们可以提供更具体的答案。
| 归档时间: |
|
| 查看次数: |
5742 次 |
| 最近记录: |