带有动画完成回调的 SwiftUI [英] SwiftUI withAnimation completion callback

查看:37
本文介绍了带有动画完成回调的 SwiftUI的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个基于某种状态的 swiftUI 动画:

I have a swiftUI animation based on some state:

withAnimation(.linear(duration: 0.1)) {
    self.someState = newState
}

有没有在上面的动画完成时触发的回调?

Is there any callback which is triggered when the above animation completes?

如果有关于如何在 SwiftUI 中使用非 withAnimation 完成块完成动画的任何建议,我也愿意接受这些建议.

If there are any suggestions on how to accomplish an animation with a completion block in SwiftUI which are not withAnimation, I'm open to those as well.

我想知道动画何时完成,以便我可以做其他事情,就本示例而言,我只想在动画完成时打印到控制台.

I would like to know when the animation completes so I can do something else, for the purpose of this example, I just want to print to console when the animation completes.

推荐答案

在这个博客上,Guy Javier 描述了如何使用 GeometryEffect 来获得动画反馈,在他的示例中,他检测动画何时达到 50% 以便他可以翻转视图并使其看起来像有 2 个边

On this blog this Guy Javier describes how to use GeometryEffect in order to have animation feedback, in his example he detects when the animation is at 50% so he can flip the view and make it looks like the view has 2 sides

这里是全文的链接,里面有很多解释:https://swiftui-lab.com/swiftui-animations-part2/

here is the link to the full article with a lot of explanations: https://swiftui-lab.com/swiftui-animations-part2/

我会在这里复制相关的片段,这样即使链接不再有效,答案仍然是相关的:

I will copy the relevant snippets here so the answer can still be relevant even if the link is not valid no more:

在这个例子中 @Binding var flipped: Bool 当角度在 90 到 270 之间时变为真,然后变为假.

In this example @Binding var flipped: Bool becomes true when the angle is between 90 and 270 and then false.

struct FlipEffect: GeometryEffect {

    var animatableData: Double {
        get { angle }
        set { angle = newValue }
    }

    @Binding var flipped: Bool
    var angle: Double
    let axis: (x: CGFloat, y: CGFloat)

    func effectValue(size: CGSize) -> ProjectionTransform {

        // We schedule the change to be done after the view has finished drawing,
        // otherwise, we would receive a runtime error, indicating we are changing
        // the state while the view is being drawn.
        DispatchQueue.main.async {
            self.flipped = self.angle >= 90 && self.angle < 270
        }

        let a = CGFloat(Angle(degrees: angle).radians)

        var transform3d = CATransform3DIdentity;
        transform3d.m34 = -1/max(size.width, size.height)

        transform3d = CATransform3DRotate(transform3d, a, axis.x, axis.y, 0)
        transform3d = CATransform3DTranslate(transform3d, -size.width/2.0, -size.height/2.0, 0)

        let affineTransform = ProjectionTransform(CGAffineTransform(translationX: size.width/2.0, y: size.height / 2.0))

        return ProjectionTransform(transform3d).concatenating(affineTransform)
    }
}

您应该能够将动画更改为您想要实现的任何内容,然后在完成后获取绑定以更改父级的状态.

You should be able to change the animation to whatever you want to achieve and then get the binding to change the state of the parent once it is done.

这篇关于带有动画完成回调的 SwiftUI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆