【问题标题】:swift: call action after implementing another actionswift:在执行另一个动作后调用动作
【发布时间】:2018-08-02 05:05:43
【问题描述】:

我有两个动作。

我想执行第一个操作,然后调用第二个操作。如果我添加这样的操作:

func actions {
    action1()
    action2()
}

但在我的代码和操作中同时实现。

我在第一个操作中有此代码:

let pageController = self.storyboard?.instantiateViewController(withIdentifier: "PageViewController") as! UIPageViewController

pageController.dataSource = self
pageController.delegate = self

firstPage = 0
secondPage = 1

guard let firstController = getContentViewController(withIndex: firstPage) else { return }
guard let secondController = getContentViewController(withIndex: secondPage) else { return }

pageViewController?.setViewControllers([firstController, secondController], direction: .reverse, animated: true, completion: nil)

这是一个翻转动画。

如果我用这个

func actions {
    action1()
    action2()
}

我的翻转动画将无法显示。

也许我需要使用计时器?

【问题讨论】:

标签: ios swift


【解决方案1】:

您可以在每个函数中包含回调,这样一旦函数操作完成,它们就会调用其他函数。例如:

创建回调函数

func action1(complete: @escaping (_ status: Bool) {
    // animation operation 

    // Notify callback 
    complete(true)
}

如何使用回调函数

action1(complete: { (animationCompleted)
   if animationCompleted {
     // Invoke second animation function 
     // Remember use `self` instance once your inside a callback method
     self.action2() 
   }
})

【讨论】:

  • 我的动画翻转动画没有显示。我需要时间来显示动画,然后调用另一个动作。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-04-12
  • 2017-05-14
  • 2018-02-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多