Michael
Michael

Reputation: 726

swift change view after animation

i have a animation with .ontapgesture, and it works fine. so far so good. but I want to call my next view after the animation is finished. but I don't know how. Is there an option to build in a delay while the animation runs?

var body: some View {
Text("test")
    .scaleEffect(x: isButtonPressed ? 1.5 : 1, y: isButtonPressed ? 1.5 : 1)
   
    .animation(Animation.linear.repeatCount(5))
    .onTapGesture {
        isButtonPressed.toggle()
        jumpToNextView.toggle()

}
}

Upvotes: 4

Views: 167

Answers (1)

davidev
davidev

Reputation: 8517

You can use DispatchQueue to execute the toggle of jumpToNextView after the animation.

Text("test")
    .scaleEffect(x: isButtonPressed ? 1.5 : 1, y: isButtonPressed ? 1.5 : 1)
    .animation(Animation.linear(duration: 0.25).repeatCount(5))
    .onTapGesture {
        isButtonPressed.toggle()
        //<< executed after 0.25 (duration) * 5 (repeat count)
        DispatchQueue.main.asyncAfter(deadline: .now() + 1.25) {
            jumpToNextView.toggle()
        }
    }

Upvotes: 1

Related Questions