通过动画延迟链接SwiftUI-仅显示最后一个动画,除非对象是显示所有动画的中间动画

时间:2020-07-03 08:31:41

标签: swift swiftui swiftui-animation

似乎在another question中,有人提出了一个解决方案,您可以将其与动画链接在一起。这对我来说很有意义,所以我尝试了一下,几乎可以了。当对象不运动时,仅显示最后一个动画。但是,如果我中断当前动画,则所有动画都会流畅播放。是否缺少步骤,或者我可能没有正确设置?

这是我的测试代码。

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()
    }
}

0 个答案:

没有答案