您现在的位置: IOS教程網 >> IOS系統 >  >> iOS 教學 >> iOS 相關教學

iOS 開發之路——實作啟動時的新功能引導頁

編輯:iOS 相關教學

新功能引導頁的關鍵判斷很單純:比對沙盒裡記下的版本號與當前 App 版本號,不同就顯示引導頁,相同就直接進入主畫面。完成引導後,用 block 把「使用者點了開始」這件事回報給 AppDelegate,由它決定下一步要去哪裡。

BeyondAppDelegate.m

//
//  BeyondAppDelegate.m
//  20_帥哥no微博
//

#import "BeyondAppDelegate.h"
#import "BeyondViewController.h"
#import "NewFeatureViewController.h"
#import "OauthViewController.h"

#define kVersionCodeKey (NSString *)kCFBundleVersionKey

@implementation BeyondAppDelegate

// 應用最先執行的部分
- (BOOL)application:(UIApplication *)application
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // 1. 建立視窗
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

    // 2. 設定視窗背景色
    self.window.backgroundColor = [UIColor whiteColor];

    // 3. 判斷是否為本版本首次使用,決定根控制器
    [self versionCodeCheck];

    // 4. 讓視窗可見
    [self.window makeKeyAndVisible];

    return YES;
}

// 判斷是否為本版本首次使用
- (void)versionCodeCheck {
    // 從沙盒取出舊的版本號
    NSString *oldVersionCode = [[NSUserDefaults standardUserDefaults] objectForKey:kVersionCodeKey];
    NSString *currentVersionCode = [[[NSBundle mainBundle] infoDictionary] objectForKey:kVersionCodeKey];

    if ([currentVersionCode isEqualToString:oldVersionCode]) {
        // 版本相同,直接進入主畫面
        [self enterBeyondVC];
    } else {
        // 版本不同,進入新功能引導頁
        // 關鍵:記下當前版本號
        [[NSUserDefaults standardUserDefaults] setObject:currentVersionCode forKey:kVersionCodeKey];

        NewFeatureViewController *newVC = [[NewFeatureViewController alloc] init];
        newVC.startBlock = ^(BOOL isSelected) {
            NSLog(@"%d", isSelected);
            [self enterBeyondVC];
        };
        self.window.rootViewController = newVC;
    }
}

- (void)enterBeyondVC {
    // 若已完成 OAuth 授權則直接進入主控制器,否則先做授權
    if (1) {
        OauthViewController *oauthVC = [[OauthViewController alloc] init];
        self.window.rootViewController = oauthVC;
        return;
    }

    BeyondViewController *vc = [[BeyondViewController alloc] init];
    self.window.rootViewController = vc;
}

@end

NewFeatureViewController.h

//
//  NewFeatureViewController.h
//

// 只在版本新功能啟動時使用一次
#import <UIKit/UIKit.h>

@interface NewFeatureViewController : UIViewController

// 點擊開始按鈕時觸發,用來讓 AppDelegate 初始化主控制器
// block 記得用 copy
@property (nonatomic, copy) void (^startBlock)(BOOL isShare);

@end

NewFeatureViewController.m

//
//  NewFeatureViewController.m
//

/*
 寫在前面,幾個實作上容易踩到的檢查清單:

 當一個控件「不顯示」時,檢查:
   1. 是不是沒有寬度和高度
   2. x / y 座標是否正確
   3. 是不是還沒被加到父控件中

 當一個控件「不能點擊」時,檢查:
   1. 自己的 userInteractionEnabled 是否為 NO
   2. 父控件的 userInteractionEnabled 是否為 NO
   3. 控件是否已超出所屬控制器的範圍

 其他備忘:
   - pageControl 的高度若無法修改,可把高度設為 0,其他控件同理
   - 居中顯示的標準步驟:先設 center,再設 bounds
   - 切換按鈕圖片可透過切換狀態完成
       normal      → 普通
       highlighted → 高亮(人為長按)
       disabled    → 失效(由程式碼控制)
       selected    → 選中(由程式碼控制)
*/

#import "NewFeatureViewController.h"

#define kPicNum 4
#define viewSize self.view.bounds.size

@interface NewFeatureViewController ()<UIScrollViewDelegate> {
    // 頁面指示器
    UIPageControl *_pageControl;
    // 因為開始按鈕的點擊事件需要讀取分享按鈕的選中狀態
    UIButton *_shareBtn;
}

@end

@implementation NewFeatureViewController

- (BOOL)prefersStatusBarHidden {
    return YES;
}

- (void)loadView {
    // 讓 imageView 成為控制器的 view,再往上面疊一層 scrollView
    UIImageView *imgView = [[UIImageView alloc] init];

    // 控制器的 view 設為全屏
    imgView.frame = [UIScreen mainScreen].bounds;

    // 使用分類方法:若是 iPhone 5 就載入 -568h 的圖片
    imgView.image = [UIImage fullScrennImageNamed:@"new_feature_background.png"];

    // 讓 view 可以與使用者互動
    imgView.userInteractionEnabled = YES;

    self.view = imgView;
}

- (void)viewDidLoad {
    [super viewDidLoad];

    // 1. 載入 scrollView,並批量加入 imageView
    [self addUIScrollView];

    // 2. 載入 UIPageControl
    [self addUIPageControl];
}

// 載入 scrollView,並批量加入每頁一張圖的 imageView
- (void)addUIScrollView {
    UIScrollView *scrollView = [[UIScrollView alloc] init];

    // 可視範圍
    scrollView.frame = self.view.bounds;
    // 隱藏水平滾動條
    scrollView.showsHorizontalScrollIndicator = NO;
    // 按可視範圍分頁
    scrollView.pagingEnabled = YES;
    // 滾動範圍
    scrollView.contentSize = CGSizeMake(kPicNum * viewSize.width, 0);
    // 監聽滾動事件
    scrollView.delegate = self;

    [self.view addSubview:scrollView];

    for (int i = 0; i < kPicNum; i++) {
        [self addImageViewToScrollView:scrollView atIndex:i];
    }
}

// 載入 UIPageControl
- (void)addUIPageControl {
    _pageControl = [[UIPageControl alloc] init];

    // 居中的最佳做法:先設 center,再設 bounds
    _pageControl.center = CGPointMake(viewSize.width * 0.5, viewSize.height * 0.95);
    _pageControl.bounds = CGRectMake(0, 0, 100, 0);

    // 總頁數
    _pageControl.numberOfPages = kPicNum;

    // 重點:從圖片取色,分別設定預設色與當前頁色
    _pageControl.pageIndicatorTintColor =
        [UIColor colorWithPatternImage:[UIImage imageNamed:@"new_feature_pagecontrol_point.png"]];
    _pageControl.currentPageIndicatorTintColor =
        [UIColor colorWithPatternImage:[UIImage imageNamed:@"new_feature_pagecontrol_checked_point.png"]];

    // 禁用預設互動
    _pageControl.userInteractionEnabled = NO;

    [self.view addSubview:_pageControl];
}

// 滾動減速結束(即 scrollView 靜止)時,同步 pageControl 的當前頁
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    // 當前頁 = 偏移的 x ÷ 單頁寬度
    _pageControl.currentPage = scrollView.contentOffset.x / scrollView.frame.size.width;
}

// 依索引把 imageView 排進 scrollView
- (void)addImageViewToScrollView:(UIScrollView *)scrollView atIndex:(int)i {
    UIImageView *imageView = [[UIImageView alloc] init];

    // 水平排列,依索引計算 frame(結構體需強轉為 CGRect)
    imageView.frame = (CGRect){{i * viewSize.width, 0}, viewSize};

    // 設定圖片
    NSString *name = [NSString stringWithFormat:@"new_feature_%d.png", i + 1];
    imageView.image = [UIImage fullScrennImageNamed:name];

    [scrollView addSubview:imageView];

    // 最後一頁要加上「分享」與「開始」兩個按鈕
    if (i == kPicNum - 1) {
        // 必須讓最後一張 imageView 可互動,內部的按鈕才點得到
        imageView.userInteractionEnabled = YES;

        [self addStartBtnInImageView:imageView];
        [self addShareBtnInImageView:imageView];
    }
}

// 最後一頁的「分享」按鈕
- (void)addShareBtnInImageView:(UIImageView *)imageView {
    _shareBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [imageView addSubview:_shareBtn];

    // 設定正常與選中狀態的背景圖,並取回圖片尺寸
    CGSize shareBtnNormalBgImgSize =
        [_shareBtn setBtnBgImgForNormalAndSelectedWithName:@"new_feature_share_false.png"
                                              selectedName:@"new_feature_share_true.png"];

    // 邊框:居中,先 center 後 bounds
    _shareBtn.center = CGPointMake(viewSize.width * 0.5, viewSize.height * 0.75);
    _shareBtn.bounds = (CGRect){CGPointZero, shareBtnNormalBgImgSize};

    [_shareBtn addTarget:self
                  action:@selector(shareBtnClick:)
        forControlEvents:UIControlEventTouchUpInside];

    // 高亮時不要改變圖片顏色
    _shareBtn.adjustsImageWhenHighlighted = NO;

    // 預設選中
    _shareBtn.selected = YES;
}

// 分享按鈕:切換選中狀態即可呈現兩張不同的圖
- (void)shareBtnClick:(UIButton *)shareBtn {
    shareBtn.selected = !shareBtn.isSelected;
}

// 最後一頁的「開始」按鈕
- (void)addStartBtnInImageView:(UIImageView *)imageView {
    UIButton *startBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [imageView addSubview:startBtn];

    // 設定背景圖等樣式,並取回圖片尺寸
    CGSize startBtnBgImgSize =
        [startBtn setBtnBgImgForNormalAndHighightedWithName:@"new_feature_finish_button.png"];

    startBtn.center = CGPointMake(viewSize.width * 0.5, viewSize.height * 0.85);
    startBtn.bounds = (CGRect){CGPointZero, startBtnBgImgSize};

    [startBtn addTarget:self
                 action:@selector(startBtnClick)
       forControlEvents:UIControlEventTouchUpInside];
}

// 點擊開始:觸發 block,交由外部初始化主控制器
- (void)startBtnClick {
    if (_startBlock) {
        _startBlock(_shareBtn.isSelected);
    }
}

@end

這裡有兩個值得記下的細節:一是引導頁用 imageView 當作控制器的 view,再往裡頭疊 scrollView,能少一層視圖階梯;二是 pageControl 的圓點顏色直接從圖片取色,不必另外準備色值。至於版本號的比對與寫回,務必放在 AppDelegate 而非引導頁,這樣才不會出現使用者跳過引導卻沒記下版本號的窘境。

  1. 上一篇文章:
  2. 下一篇文章:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © IOS教程網 All Rights Reserved