在項(xiàng)目中我時(shí)常會(huì)遇到一些彈窗類(lèi)提示,產(chǎn)品說(shuō)讓它突然顯示太突兀了,我夸?一個(gè)嘴巴子,“你咋那么多事”。不過(guò)我心善就勉強(qiáng)接受他的建議加了點(diǎn)動(dòng)畫(huà),在此就用到下面的一些延時(shí)操作。
現(xiàn)階段我共知道四種能完成上述要求的方法:
1.NSObject方法
[self performSelector:@selector(delayMethod) withObject:nil afterDelay:1.0f];
主線程中執(zhí)行;
非阻塞的執(zhí)行方式;
可以通過(guò)cancelPreviousPerformRequestsWithTarget取消執(zhí)行。
2.定時(shí)器:NSTimer
[NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(delayMethod) userInfo:nil repeats:NO];
主線程執(zhí)行;
非阻塞的執(zhí)行方式;
可以通過(guò)NSTimer類(lèi)的- (void)invalidate;取消執(zhí)行。
3.NSThread
[NSThread sleepForTimeInterval:1.0];
[self delayMethod];
主線程和子線程均可執(zhí)行;
一種阻塞的執(zhí)行方式,因此建議放在子線程中執(zhí)行;
暫時(shí)未有取消執(zhí)行的方法。
4.GCD
double delayInSeconds = 1.0;
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
// 小提示此為延遲提交 不是延遲執(zhí)行
dispatch_after(popTime, dispatch_get_main_queue(), ^{
[self delayMethod];
});
通過(guò)參數(shù)選擇在主線程或子線程中執(zhí)行;
非阻塞的執(zhí)行方法;
暫時(shí)未有取消執(zhí)行的方法
DEMO
建議將該效果進(jìn)行封裝,程序員要多心疼自己,少寫(xiě)點(diǎn)是點(diǎn)0-0.
#pragma mark --移除活動(dòng)規(guī)則View
- (void)removeRulesClick {
UIButton *button = [self.view viewWithTag:2222];
[UIView animateWithDuration:0.5 animations:^{
button.alpha = 0;
rulesView.alpha = 0;
}];
[self performSelector:@selector(removeRulesView) withObject:nil afterDelay:0.5f];
}
- (void)removeRulesView {
UIButton * button = [self.view viewWithTag:2222];
[button removeFromSuperview];
[rulesView removeFromSuperview];
}
總結(jié)完上面,突然感覺(jué)自己子線程這塊記憶有點(diǎn)模糊了,下一篇應(yīng)該會(huì)寫(xiě)一點(diǎn)子線程方面的東西吧0_0。