快速更改动画后的视图

4

我有一个使用.ontapgesture的动画效果,它运行得很好。到目前为止一切都很好。 但是我想在动画完成后调用我的下一个视图。但我不知道如何做。在动画运行时是否有一种内置延迟选项?

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

}
}

这可能会有所帮助:https://www.avanderlee.com/swiftui/withanimation-completion-callback/ - Raja Kishan
嗨,Raja,非常感谢。我会去看看这个网站! - Michael
1个回答

1
你可以使用DispatchQueue在动画后执行跳转到下一个视图的切换。
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()
        }
    }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接