您现在的位置: IOS教程網 >> IOS系統 >  >> iOS 程式開發 >> iOS 開發綜合

iOS 開發實戰(八):用 UIScrollView 打造新版本特性頁

編輯:iOS 開發綜合

一、什麼時候該顯示新特性

使用者第一次開啟某個新版本的 App 時,通常會看到幾張介紹本次更新的引導圖。判斷條件不是「第一次安裝」,而是「第一次使用這個版本」——這兩者並不相同,因為舊使用者升級後同樣需要看到新特性。

版本號可以在 plist 的 bundle version 欄位找到。至於判斷邏輯,則寫在 AppDelegate 的啟動方法裡:把每次啟動時的版本號存進沙盒,下次啟動時取出上次的紀錄做比對。若兩者相同,代表老手,直接進首頁;若不同,就把引導頁設為 window 的根控制器。

二、版本比對的程式碼

- (BOOL)application:(UIApplication *)application
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    self.window = [[UIWindow alloc] init];
    self.window.frame = [UIScreen mainScreen].bounds;

    NSString *versionKey = (__bridge NSString *)kCFBundleVersionKey;

    // 取出上次保存的版本號
    NSUserDefaults *defaults = [[NSUserDefaults alloc] init];
    NSString *lastVersion = [defaults objectForKey:versionKey];

    // 取得目前 this build 的版本號
    NSString *currentVersion = [NSBundle mainBundle].infoDictionary[versionKey];

    if ([currentVersion isEqualToString:lastVersion]) {
        self.window.rootViewController = [[YYTabBarViewController alloc] init];
    } else {
        self.window.rootViewController = [[YYNewfeatureViewController alloc] init];
        [defaults setObject:currentVersion forKey:versionKey];
        [defaults synchronize]; // 立刻寫入,別等系統排程
    }

    [self.window makeKeyAndVisible];
    return YES;
}

三個容易踩到的細節

第一,版本號比較不需要轉成浮點數。版本字串本來就不是數字語意,只要「不一樣」就是新版本,直接做字串比較即可。

第二,bundle version 在 Xcode 中的真實型別是 CFStringRef,也就是 Core Foundation 的型別,因此取值時需要用 __bridge 做橋接轉換,才能交給 NSString 使用。

第三,Foundation 與 Core Foundation 的型別之間確實可以互轉,例如 NSString 對應 CFStringRef、NSArray 對應 CFArrayRef、NSDictionary 對應 CFDictionaryRef,但一律必須透過 __bridge 保留記憶體管理的一致性。

三、引導頁的實作

- (void)setupScrollView {
    UIScrollView *scrollView = [[UIScrollView alloc] init];
    scrollView.frame = self.view.bounds;
    scrollView.delegate = self;
    [self.view addSubview:scrollView];

    CGFloat imageW = scrollView.width;
    CGFloat imageH = scrollView.height;

    for (int i = 0; i < YYNewfeatureImageCount; i++) {
        UIImageView *imageView = [[UIImageView alloc] init];
        NSString *name = [NSString stringWithFormat:@"new_feature_%d", i + 1];
        if (FourInch) {
            name = [name stringByAppendingString:@"-568h"];
        }
        imageView.image = [UIImage imageWithName:name];
        [scrollView addSubview:imageView];

        imageView.x = i * imageW;
        imageView.y = 0;
        imageView.width = imageW;
        imageView.height = imageH;
    }

    scrollView.contentSize = CGSizeMake(YYNewfeatureImageCount * imageW, 0);
    scrollView.backgroundColor = YYColor(246, 246, 246);
    scrollView.showsHorizontalScrollIndicator = NO;
    scrollView.bounces = NO;
}

這裡有四個設定值得說明:contentSize 決定可滑動的範圍,寬度必須是「圖片數 × 單張寬度」;背景色建議與設計稿的底色一致,避免翻頁邊緣露出突兀的白底;隱藏水平滾動條是為了畫面乾淨;關閉 bounces 則是為了避免使用者把頁面拖出界外,那在引導頁這種場合會顯得不專業。

四、頁碼同步

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    double doublePage = scrollView.contentOffset.x / scrollView.width;
    int intPage = (int)(doublePage + 0.5); // 加 0.5 取整等於四捨五入
    self.pageControl.currentPage = intPage;
}

頁碼控件的位置通常放在畫面下方固定距離處,並為「目前頁」與「其他頁」設定不同顏色,讓使用者一眼就知道自己在第幾張。最後別忘了覆寫 prefersStatusBarHidden 回傳 YES,把狀態列藏起來,引導頁的沉浸感會完整很多。

五、把常用宏放進 pch

專案裡會反覆用到的判斷,例如是否為 iOS 7、是否為 4 吋螢幕、顏色與隨機色,建議集中寫在 Prefix.pch 中,全域可用。

#ifdef DEBUG
#define YYLog(...) NSLog(__VA_ARGS__)
#else
#define YYLog(...)
#endif

#define YYColor(r, g, b) [UIColor colorWithRed:(r)/255.0 green:(g)/255.0 blue:(b)/255.0 alpha:1.0]
#define iOS7 ([[UIDevice currentDevice].systemVersion doubleValue] >= 7.0)
#define FourInch ([UIScreen mainScreen].bounds.size.height == 568.0)
  1. 上一篇文章:
  2. 下一篇文章:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © IOS教程網 All Rights Reserved