如何等到 UIView 过渡结束 [英] How to wait till a UIView transition is over

查看:37
本文介绍了如何等到 UIView 过渡结束的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这与 我之前的问题有关.

我有以下方法来移动扑克牌.

I have the following method where it moves a playing card.

func moveCard() -> Void{
  UIView.animate(withDuration: 5.0) {
    self.hhview.transform = CGAffineTransform(translationX: -160, y: 0)
  } completion: { _ in
    print("Move completed")
  }
}

这个方法被不同的父方法调用.但是其余的父方法在转换完成之前执行.例如,在Move completed之前打印playerCheck continue.

This method is called by different parent methods. But the rest of the parent methods are executed before the completion of the transition. For example, playerCheck continued is printed before Move completed.

func playerCheck() -> Void{
    ......
    moveCard()

    // how to wait till completion of the transition
    print("playerCheck continued")
}

如何设置父方法在执行其余功能之前等待转换完成?

How can I set the parent methods to wait till the transformation is completed before executing the rest of the functionality?

推荐答案

使 moveCard 接受一个完成处理程序,并将所有代码after"放在moveCard 在完成处理程序中:

Make moveCard accept a completion handler, and put all the code "after" moveCard in the completion handler:

func moveCard(completion: () -> Void) -> Void{
    UIView.animate(withDuration: 5.0) {
        self.hhview.transform = CGAffineTransform(translationX: -160, y: 0)
    } completion: { _ in
        print("Move completed")
        completion()
    }
}

moveCard {
    print("playerCheck continued")
}

如果您要调用另一个具有完成处理程序的方法,则在完成处理程序中,可能会有相当多的嵌套.您可以考虑使用 PromiseKit,您可以在其中使用 then 将这些链接起来.

If you are going to call another method that has a completion handler, in the completion handler, there might be quite a lot of nesting. You might consider using PromiseKit, where you can use then to chain these.

这篇关于如何等到 UIView 过渡结束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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