動態(tài) iOS 啟動廣告圖

按照蘋果的官方的建議,啟動圖最好是不填充數(shù)據(jù)的app首頁的截圖,這樣可以讓用戶感覺應(yīng)用的響應(yīng)速度很快,但是這個建議現(xiàn)在已經(jīng)很少有app執(zhí)行了,我本人更推崇蘋果官方的做法,但是無奈項目要求,所以……

廢話不多說。一下內(nèi)容都是我的實現(xiàn),僅供參考

首先制作一個在線配置文件,我是將自己需要的參數(shù)寫成json文件放在服務(wù)器上。請求這個配置文件和請求接口一樣。

文件內(nèi)容如下

{
  "errno" : 0,
  "result" : {
    "launch_image" : {
        "show" : 1, 
        "micro" : "http://test.test/1.jpg",
        "small" : "http://test.test/2.jpg",
        "middle" : "http://test.test/3.jpg",
        "big" : "http://test.test/4.jpg"
      }
  }
}

簡單解釋一下,show在本地解析為BOOL值,1代表有動態(tài)啟動圖要顯示;micro、small、middle、big分別對應(yīng)3.5寸、4寸、4.7寸和5.5寸的設(shè)備所需的圖片的地址。

確保這個文件能被你請求到并正確解析,接下來先考慮顯示啟動圖的邏輯。

先上代碼:

    //取到已經(jīng)下載好的啟動圖片的路徑
    NSString *path = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES).lastObject;
    NSString *fileName = [path stringByAppendingPathComponent:[[NSUserDefaults standardUserDefaults] valueForKey:@"launchImage"]];
    //將圖片文件初始化
    UIImage *img = [UIImage imageWithContentsOfFile:fileName];
    //如果有圖片存在
    if (img) {
        //初始化一個imageView,并添加到window上
        UIImageView *imgView = [[UIImageView alloc]initWithFrame:[UIApplication sharedApplication].keyWindow.bounds];
        imgView.image = img;
        [self.window addSubview:imgView];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [imgView removeFromSuperview];
        });
    }

上面的代碼應(yīng)該寫在AppDelegate.m的- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions方法中,每次app啟動的時候都會去緩存文件中找文件名是預(yù)先存在NSUserDefaults中的key為launchImage的值的文件,如果找到了就將imageView添加到window上面,過三秒再移除。

第二部分是更新下載啟動圖的邏輯,這部分代碼最好寫在app每次啟動必經(jīng)的controller里面,比如tabbarController或者rootNavigationController里面,保證每次app啟動都能運行這段代碼,檢查和下載啟動圖片。

NSInteger screenHeight = [UIScreen mainScreen].bounds.size.height;
CGFloat scale = 0;
NSString *name;
switch (screenHeight) {
      case 480:
      name = info.launch_image.micro;
      scale = 2;
      break;
      case 568:
      name = info.launch_image.small;
      scale = 2;
      break;
      case 667:
      name = info.launch_image.middle;
      scale = 2;
      break;
      case 736:
      name = info.launch_image.big;
      scale = 3;
      break;
}
if (info.launch_image.show) {
    [UIImage downLoadImage:name scale:scale success:^(UIImage *img) {
        NSLog(@"launchImage download Success");
        [[NSUserDefaults standardUserDefaults]setValue:name.lastPathComponent forKey:@"launchImage"];
    }];
}else{
      NSString *path = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES).lastObject;
      NSString *fileName = [path stringByAppendingPathComponent:name.lastPathComponent];
      if ([[NSFileManager defaultManager]fileExistsAtPath:fileName]) {
           NSError *error;
           if ([[NSFileManager defaultManager] removeItemAtPath:fileName error:&error]) {
                NSLog(@"remove file success");
           }else{
                NSLog(@"%@", error);
           }
      }
}

簡單說明一下,這段代碼里面的info就是之前寫好的配置文件,請求成功后對象化的數(shù)據(jù)。第一部分到switch語句結(jié)束都是為了通過當(dāng)前屏幕的寬度判斷需要下載哪一個圖片文件。通過配置文件的show字段來判斷是否有文件需要下載,有的話就下載下來,并且在NSUserDefaults中設(shè)置一個key為launchImage值為所下載文件的文件名的字段。如果show的值是NO的話就找到緩存的文件刪除之。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容