最新消息:VPS服务器又从fzhost.net换回Linode了,主题仍用朋友推荐的大前端D8

[已解决]swift延迟执行代码做事情

Swift crifan 178浏览 0评论
                //has sent the friend invitation to user
                //wait user accept the friend invitation
                dispatch_async(dispatch_get_main_queue(), { () -> Void in
//                    self.view.makeToast(message: "添加人脉邀请已发送")
                    self.view.makeToast(message: "添加人脉邀请已发送", duration: 0.5, position: HRToastPositionDefault)
                    //pop out previous many vc, goto root vc:
                    //[0] <JianDao.MainViewController: 0x7f80db037600>
                    self.navigationController?.popToRootViewControllerAnimated(true)
                    //select the contact vc
                    gMainVC.selectedIndex = 1
                })

希望延迟一段时间,再去做事情。

swift delay execution

ios – dispatch_after – GCD in swift? – Stack Overflow

ios – Swift performSelector: withObject: afterDelay: – Stack Overflow

objective c – Adding delay between execution of two following lines – Stack Overflow

xcode – How to create a delay in Swift? – Stack Overflow

Delaying code execution

最后用:

func delayDispatchInMainThread(delayTimeInSec:Double, thingsTodo:()->()) {
    let dispatchDelayTime = dispatch_time(
        DISPATCH_TIME_NOW,
        Int64(delayTimeInSec * Double(NSEC_PER_SEC))
    )
    dispatch_after(dispatchDelayTime, dispatch_get_main_queue(), thingsTodo)
}
                    let deayTimeInSec:Double = 1
                    self.view.makeToast(message: "添加人脉邀请已发送", duration: deayTimeInSec, position: HRToastPositionCenter)
                    //TODO: add current contact into contact list
                    delayDispatchInMainThread(deayTimeInSec, thingsTodo: self.closeCurrentAndReturnToMain)
    func closeCurrentAndReturnToMain(){
       //do your things
    }

即可。

转载请注明:在路上 » [已解决]swift延迟执行代码做事情

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
18 queries in 0.242 seconds, using 9.88MB memory