Search code examples
swiftanimationpromiseios-animationsspring-swift

Chaining animations in Swift with Spring won't work properly


I've tried chaining animations with Spring, but sadly the docs are pretty unclear about chaining animations (https://github.com/MengTo/Spring).

I have also seen that some people asked the same question but somehow, their answers won't work for me. (Spring Meng To chaining animations?, https://github.com/MengTo/Spring/issues/123)

I have tried using the chain like in the Spring examples :

func startAnimationSpringChain()  {
    self.springView.animation = "slideRight"
    self.springView.animateNext {
        self.springView.animation = "fadeOut"
        self.springView.animate()
    }
}

Here the first animation works but the second one is a combination of the "fadeOut" and a reversed played "slideRight".

Then I tried using Promises of PromiseKit (https://github.com/mxcl/PromiseKit):

func startAnimationPromise()  {
    firstly {
        self.firstAnimation()
        }.done {
            self.secondAnimation()
    }
}

func firstAnimation () -> Promise<Void>{
    return Promise { seal in
        self.springView.animation = "SlideRight"
        self.springView.animate()
        seal.fulfill(())
    }
}

func secondAnimation() -> Promise<Void>{
    return Promise { seal in
        self.springView.animation = "fadeOut"
        self.springView.animate()
        seal.fulfill(())
    }
}

This resulted in only playing the second animation "fadeOut" really fast.

Can someone please explain these three functions:

animateNext { ... }
animateTo()
animateToNext { ... }

because I find it pretty hard to understand what exactly they do.

Thank you for your help, I appreciate it very much and I'm happy about all kind of advice about my code! :)

Best


Solution

  • I use animateToNext method for chaining animations. It seems animateNext does not wait for end of previous animation. Something like this:

    self.springView.animation = Spring.AnimationPreset.SlideRight.rawValue
    self.springView.animateToNext {
            self.springView.animation = Spring.AnimationPreset.FadeOut.rawValue
            self.springView.animate()
    }
    

    You also see this answer.

    However it seems not all animations can work properly when using them in one animation chain.