第一种:
[uiview animatewithduration:3 delay:3 options:1 animations:^{ self.btn.transform = cgaffinetransformmaketranslation(300, 400); } completion:^(bool finished) { nslog(@"view animation结束"); }];//不会阻塞线程,animations block中的代码对于是支持animation的代码,才会有延时效果,对于不支持animation的代码 则 不会有延时效果
第二种:
[nsthread sleepfortimeinterval:3];//阻塞线程,浪费性能 ,一般不推荐用。此方式在主线程和子线程中均可执行。 建议放到子线程中,以免卡住界面,没有找到取消执行的方法。 [self delaymethod];
dispatch_after(dispatch_time(dispatch_time_now, (int64_t)(3 * nsec_per_sec)), dispatch_get_main_queue(), ^{ });//定制了延时执行的任务,不会阻塞线程,在主线程和子线程中都可以,效率较高(推荐使用)。此方式在可以在参数中选择执行的线程。 是一种非阻塞的执行方式, 没有找到取消执行的方法。
[self performselector:@selector(test) withobject:nil afterdelay:3];//此方式要求必须在主线程中执行,否则无效。 是一种非阻塞的执行方式. [[self class] cancelpreviousperformrequestswithtarget:self];//取消本类中执行的performselector:方法
第五种:定时器
1)nstimer
[nstimer scheduledtimerwithtimeinterval:3.0f target:self selector:@selector(delaymethod) userinfo:nil repeats:no];//此方式要求必须在主线程中执行,否则无效。 是一种非阻塞的执行方式, 可以通过nstimer类的- (void)invalidate;取消执行。
2)dispatch_source_t(比 nstimer 更准的定时器),也可以在子线程中执行,非阻塞执行方式
dispatch_queue_t queue = dispatch_get_global_queue(0, 0); self.timer = dispatch_source_create(dispatch_source_type_timer, 0, 0, queue); //开始时间 dispatch_time_t start = dispatch_time(dispatch_time_now, 3.0 * nsec_per_sec); //间隔时间 uint64_t interval = 2.0 * nsec_per_sec; dispatch_source_set_timer(self.timer, start, interval, 0); //设置回调 dispatch_source_set_event_handler(self.timer, ^{ [self delaymethod]; dispatch_suspend(self.timer); }); //启动timer dispatch_resume(self.timer);
如对本文有疑问, 点击进行留言回复!!
APP调用微信小程序,能拉起小程序,但是onResp回调不会被调用问题
Codeforces Round #657 (Div. 2) B. Dubious Cyrpto(思维,数学)
网友评论