【问题标题】:Delay in DispatchQueue.main.asyncDispatchQueue.main.async 中的延迟
【发布时间】:2018-06-21 08:50:14
【问题描述】:

我有一个如下流程的设计。我需要在第 5 步和第 6 步之间设置延迟 0.3 秒。我尝试了以下选项,但没有得到任何结果。

我的问题是,我怎样才能做到这一点?

注意:观看动画需要 13 秒。

流程

  1. Task Handler // 用于 webService 请求
  2. 关闭处理程序 // 用于触发器 ViewController
  3. DispatchQueue.main.async // 用于更新 UI
  4. 第一个动画
  5. 第二部动画
  6. 导航到下一个屏幕

测试 1

Timer.scheduledTimer(withTimeInterval: 13, repeats: false, block: {})

测试 2

UIView.animate(withDuration: 13, animations: {
    // nothing should be happened
    self.ivSuccessMark.alpha = 0.99 // for dummy animation diff
}, completion: { (completion) in
    // navigation
})

测试 3

perform(_:with:afterDelay:)

【问题讨论】:

  • UIView.animateWithDuration:delay:options:animations:completion: ?
  • 使用动画完成处理程序

标签: swift dispatch-queue


【解决方案1】:

试试这个

UIView.animate(withDuration: 0.5, delay: 0.3, options: [.repeat, .curveEaseOut, .autoreverse], animations: {
    // animation stuff      
}, completion: { _ in
    // do stuff once animation is complete
})

【讨论】:

  • 尝试了下面的代码,但不起作用。 UIView.animate(withDuration: 0.3, delay: 13, options: UIViewAnimationOptions.curveLinear, animations: { }, completion: { (completion) in })
【解决方案2】:

试试这个,希望对你有帮助(最多 4 秒停止所有操作)

DispatchQueue.main.asyncAfter(deadline: .now() + .seconds(4), execute: {
    // Put your code which should be executed with a delay here
})

【讨论】:

    猜你喜欢
    • 2018-06-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-01
    • 1970-01-01
    • 2017-07-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多