4

It appears that in another question, someone gave a solution that you could chain withAnimations together. It makes sense to me so I tried it and it almost works. While the object is not in motion, only the very last animation is displayed. However, if I interrupt the current animation, all animations play smoothly. Is there a missing step or perhaps I'm not setting something up properly?

Here's my test code.

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 Answers0