【发布时间】:2021-08-25 10:38:49
【问题描述】:
在我的应用程序中,我有一个按钮,其动画表示应用程序当前正在执行与此按钮关联的功能。
一切正常,直到需要在屏幕上隐藏/显示按钮(或包含它的视图)。 当按钮返回屏幕时,按钮动画停止工作。我理解为什么会发生这种情况(没有需要动画的状态变化),但我不明白它是如何容易解决的((
最终,我希望按钮动画始终仅取决于动画显示的状态,无论按钮隐藏/显示多少次。
struct ContentView: View {
@State private var animationOn = false
@State private var showButton = true
let buttonAnimation = Animation.easeIn(duration: 1).repeatForever(autoreverses: false)
var body: some View {
VStack {
Toggle("Show button", isOn: $showButton)
.padding()
if showButton {
Image(systemName: "face.smiling")
.imageScale(.large)
.overlay(
Circle()
.stroke(Color.blue, lineWidth: animationOn ? 5 : 0)
.scaleEffect(animationOn ? 2 : 1)
.opacity(animationOn ? 0 : 1)
.animation(animationOn ? buttonAnimation : Animation.default)
)
.onTapGesture {
animationOn.toggle()
}
}
Text("animation is \(animationOn ? "on" : "off")")
.padding()
}
}
}
【问题讨论】:
-
这是否回答了您的问题stackoverflow.com/a/62072209/12299030?