隐式动画
隐式动画通过修改modifier参数,每当视图的某个 animatable 参数改变时,SwiftUI 会动画化呈现旧值到新值的变化。
@State private var animationAmount: Bool = false
var body: some View {
Image("turtlerock")
.frame(width: 300, height: 300, alignment: .center)
.clipShape(Circle())
.overlay(Circle().stroke(.white,lineWidth: 4))
.shadow( radius: 10)
.rotation3DEffect(Angle(degrees: animationAmount ? 180 : 0), axis: (x: 0.0, y: 1.0, z: 0.0))
.animation(.interpolatingSpring(stiffness: 30, damping: 3))
.onTapGesture {
self.animationAmount.toggle()
}
}
显式动画
显式动画通过 withAnimation { ... }
闭包指定。动画显示的内容都在闭包中描述。
@State private var animationAmount: Bool = false
var body: some View {
Image("turtlerock")
.frame(width: 300, height: 300, alignment: .center)
.clipShape(Circle())
.overlay(Circle().stroke(.white,lineWidth: 4))
.shadow( radius: 10)
.rotation3DEffect(Angle(degrees: animationAmount ? 180 : 0), axis: (x: 0.0, y: 1.0, z: 0.0))
.onTapGesture {
withAnimation(.interpolatingSpring(stiffness: 30, damping: 3)) {
self.animationAmount.toggle()
}
}
}