似乎在另一个问题中,有人给出了一个可以将 withAnimations 链接在一起的解决方案。这对我来说很有意义,所以我尝试了它,它几乎可以工作。当对象不运动时,仅显示最后一个动画。但是,如果我中断当前动画,所有动画都可以顺利播放。是否缺少步骤或者我没有正确设置某些东西?
这是我的测试代码。
import SwiftUI
struct AnimObject: View {
@State var position = CGPoint(
x: AnimObject.screenWidth,
y: AnimObject.screenHeight / 2)
var body: some View {
VStack {
Circle()
.frame(width: 20, height: 20, alignment: .center)
.position(position)
Button(action: {
withAnimation(Animation.linear(duration: 1.0).delay(0.0)) {
self.position.x = CGFloat(0)
}
withAnimation(Animation.linear(duration: 1.0).delay(1.0)) {
self.position.x = AnimObject.screenWidth / 2
}
withAnimation(Animation.linear(duration: 1.0).delay(2.0)) {
self.position.x = CGFloat(0)
}
withAnimation(Animation.linear(duration: 1.0).delay(3.0)) {
self.position.x = AnimObject.screenWidth
}
}) {
Text("Button")
} // Button
} // VStack
} // body
}
extension AnimObject {
static let screenWidth = UIScreen.main.bounds.size.width
static let screenHeight = UIScreen.main.bounds.size.height
static let screenSize = UIScreen.main.bounds.size
}
struct AnimObject_Previews: PreviewProvider {
static var previews: some View {
AnimObject()
}
}