如何延迟执行?
how to delay a execution?
我正在创建一个小项目,这样在按下一个按钮后
将使用导航控制器带我到下一个视图控制器 - 我可以延迟一段时间后执行导航控制器的功能
我正在使用这个代码-
import UIKit
class ViewController: UIViewController
{
@IBAction func enterButtonPressed(_ sender: UIButton)
{
let vc = storyboard?.instantiateViewController(withIdentifier: "ViewController2") as! ViewController2
self.navigationController?.pushViewController(vc, animated: true)
}
override func viewDidLoad()
{
super.viewDidLoad()
}
override func didReceiveMemoryWarning()
{
super.didReceiveMemoryWarning()
}
}
试试这个
var timeLimit = 5.0
DispatchQueue.main.asyncAfter(deadline: .now() + timeLimit, execute: {
//Play your audio here
//Audio will play after 5 seconds here
})
假设 sender.tag
持有持续时间,持续时间为 0,持续时间为 5 或 10
var playTimer:Timer?
//
@IBAction func btnClicked(_ sender:UIButton) {
playTimer?.invalidate()
playTimer = Timer.scheduledTimer(withTimeInterval: TimeInterval(sender.tag), repeats: false, block: { (T) in
// play the audio
})
}
我正在创建一个小项目,这样在按下一个按钮后 将使用导航控制器带我到下一个视图控制器 - 我可以延迟一段时间后执行导航控制器的功能
我正在使用这个代码-
import UIKit
class ViewController: UIViewController
{
@IBAction func enterButtonPressed(_ sender: UIButton)
{
let vc = storyboard?.instantiateViewController(withIdentifier: "ViewController2") as! ViewController2
self.navigationController?.pushViewController(vc, animated: true)
}
override func viewDidLoad()
{
super.viewDidLoad()
}
override func didReceiveMemoryWarning()
{
super.didReceiveMemoryWarning()
}
}
试试这个
var timeLimit = 5.0
DispatchQueue.main.asyncAfter(deadline: .now() + timeLimit, execute: {
//Play your audio here
//Audio will play after 5 seconds here
})
假设 sender.tag
持有持续时间,持续时间为 0,持续时间为 5 或 10
var playTimer:Timer?
//
@IBAction func btnClicked(_ sender:UIButton) {
playTimer?.invalidate()
playTimer = Timer.scheduledTimer(withTimeInterval: TimeInterval(sender.tag), repeats: false, block: { (T) in
// play the audio
})
}