protocol DelayProtocol { }
extension DelayProtocol where Self: UIViewController {
func delay(_ time: Double, handler: @escaping ()->()) {
DispatchQueue.main.asyncAfter(deadline: DispatchTime.now()+time, execute: handler)
}
}
使用方法:
UIViewController遵循DelayProtocol代理, 然后就可以在viewDidLoad方法中使用了。
//避免循环引用
weak var weakself = self
delay(2) {
weakself?.myObject = "Hello George"
}