iOS 工具集

iOS 实现有效延时启动页

2019-04-17  本文已影响0人  CarrySniper

  目前国内App有这样的需要:在App启动的时候加定时广告,为增加曝光度多显示几秒的启动页。但是系统默认启动页显示时间是很短的,有时候根本看不清里面的内容是什么。
  为了避免启动页一闪而过,加长启动页展示时间,我们可以加延指令,让启动页展示的同时,也可以在背后处理网络加载数据,达到启动后可以直接看的数据内容。

有效代码:

- (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 {
        // 设置1.0秒检测做一次do...while的循环
        [[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:1.0]];
    } while (!done);
    
    return YES;
}
BOOL done;
- (void)timerFired:(NSTimer *)timer {
    done = YES;
}

解释说明:只要done==NO,就一直while循环让application:didFinishLaunchingWithOptions:没有回调
优点:延时过程,可以预加载UI,也可以预请求网络数据。可以实现网络请求完成再显示首页,期间则一直显示启动页。

有效,但不理想代码:

// 启动图片延时: 1秒
[NSThread sleepForTimeInterval:1];

缺点:1、阻断线程,其操作也要等待这段时间过了才执行。2、代码顺序问题,可能导致卡屏现象。

上一篇 下一篇

猜你喜欢

热点阅读