最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

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

Swift crifan 2279浏览 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,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
94 queries in 0.329 seconds, using 22.11MB memory