??目前國內(nèi)App有這樣的需要:在App啟動的時候加定時廣告耕漱,為增加曝光度多顯示幾秒的啟動頁龙优。但是系統(tǒng)默認(rèn)啟動頁顯示時間是很短的做粤,有時候根本看不清里面的內(nèi)容是什么浇借。
??為了避免啟動頁一閃而過,加長啟動頁展示時間驮宴,我們可以加延指令逮刨,讓啟動頁展示的同時呕缭,也可以在背后處理網(wǎng)絡(luò)加載數(shù)據(jù)堵泽,達到啟動后可以直接看的數(shù)據(jù)內(nèi)容修己。
有效代碼:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
// 啟動圖片延時: 2秒
NSTimeInterval delayDuration = 2.0;
NSTimer *connectionTimer = [NSTimer scheduledTimerWithTimeInterval:delayDuration target:self selector:@selector(timerFired:) userInfo:nil repeats:NO];
[[NSRunLoop currentRunLoop] addTimer:connectionTimer forMode:NSDefaultRunLoopMode];
do {
// 設(shè)置1.0秒檢測做一次do...while的循環(huán)
[[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:1.0]];
} while (!done);
return YES;
}
BOOL done;
- (void)timerFired:(NSTimer *)timer {
done = YES;
}
解釋說明:只要done==NO,就一直while循環(huán)讓application:didFinishLaunchingWithOptions:沒有回調(diào)
優(yōu)點:延時過程迎罗,可以預(yù)加載UI睬愤,也可以預(yù)請求網(wǎng)絡(luò)數(shù)據(jù)∥瓢玻可以實現(xiàn)網(wǎng)絡(luò)請求完成再顯示首頁尤辱,期間則一直顯示啟動頁。
有效厢岂,但不理想代碼:
// 啟動圖片延時: 1秒
[NSThread sleepForTimeInterval:1];
缺點:1光督、阻斷線程,其操作也要等待這段時間過了才執(zhí)行塔粒。2结借、代碼順序問題,可能導(dǎo)致卡屏現(xiàn)象卒茬。