【发布时间】:2022-12-09 04:21:36
【问题描述】:
我正在学习 SwiftUI 动画,我有一个新手问题。 .animation() 修饰符已在 iOS 15 中弃用。要使动画正常工作,必须更改“值”。通过我对示例的更改,如果没有“值”,动作会变得不稳定并且不那么流畅。 .animation() 已弃用,但仍会出现警告。
我做对了吗?
struct Example4: View {
@State private var bounceBall: Bool = false
@State private var hiddenText: String = "Kick the ball!"
var body: some View {
VStack {
Text(hiddenText)
Image("ball")
.resizable()
.frame(width: 150, height: 150)
.clipShape(Circle())
// *Original*
.animation(Animation.interpolatingSpring(stiffness: 90, damping: 1.5).repeatForever(autoreverses: false))
// *Modified*
.animation(Animation.interpolatingSpring(stiffness: 90, damping: 1.5).repeatForever(autoreverses: false), value: bounceBall)
.offset(y: bounceBall ? -200 : 200)
.onTapGesture {
self.bounceBall.toggle()
self.hiddenText = ""
}
}
.navigationBarTitle("Example 4")
}
}
【问题讨论】:
-
.animation()已弃用,您很快就不能使用它,可能在 iOS 17 或 18 中 -
@NhatNguyenDuc 谢谢。是的我明白。这就是为什么我想知道修复方法。当我添加“value: bounceBall”时,动作是不稳定的,从我搜索的所有讨论来看,我的更改似乎应该有效。我的机器有点旧,所以可能是硬件问题?