我想制作带有偏移的抖动效果,希望在 30.0 和 -30.0 之间的起始和抖动处具有零偏移,覆盖此偏移:-30.0, 0.0, 30.0
但在编码中我不能有这 3 个选项,而我有 2 个选项,我的目标是从 0.0 开始,到 30.0,然后到 -30.0,最后到 0.0
struct ContentView: View {
@State private var start: Bool = false
var body: some View {
Image(systemName: "exclamationmark.triangle")
.font(Font.system(size: 50))
.offset(x: start ? 30.0 : -30.0)
.padding()
.animation(Animation.spring(response: 0.2, dampingFraction: 0.2, blendDuration: 0.2), value: start)
Button("Shake") {
start.toggle()
}
}
}
Run Code Online (Sandbox Code Playgroud)
Chr*_*isR 14
您可以对按钮使用显式动画而不是隐式动画。然后在按钮中拳头在没有动画的情况下按偏移量移动,然后在有动画的情况下向后移动:
struct ContentView: View {
@State private var start: Bool = false
var body: some View {
VStack {
Image(systemName: "exclamationmark.triangle")
.font(Font.system(size: 50))
.offset(x: start ? 30 : 0)
.padding()
Button("Shake") {
start = true
withAnimation(Animation.spring(response: 0.2, dampingFraction: 0.2, blendDuration: 0.2)) {
start = false
}
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1782 次 |
| 最近记录: |