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

iOS 開發:實作 App 首次啟動時顯示使用者引導頁

編輯:iOS 相關資訊

很多 App 在第一次開啟時,會先顯示幾頁新用戶引導(onboarding),過完就不再出現。這個功能的關鍵就在於「如何判斷 App 是否為第一次啟動」,而這可以透過 NSUserDefaults 輕鬆完成。

一、建立引導頁(UserGuideViewController)

建立一個繼承自 UIViewController 的類別,基本上以 UIScrollView 橫向分頁來承載多張引導圖:

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor redColor];
    [self initGuide];
}

- (void)initGuide
{
    UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 640)];
    [scrollView setContentSize:CGSizeMake(1280, 0)]; // 四頁寬度
    [scrollView setPagingEnabled:YES];               // 整頁顯示

    // 依序加入四張引導圖
    UIImageView *imageview = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 460)];
    [imageview setImage:[UIImage imageNamed:@"0.png"]];
    [scrollView addSubview:imageview];
    [imageview release];

    UIImageView *imageview1 = [[UIImageView alloc] initWithFrame:CGRectMake(320, 0, 320, 460)];
    [imageview1 setImage:[UIImage imageNamed:@"1.png"]];
    [scrollView addSubview:imageview1];
    [imageview1 release];

    UIImageView *imageview2 = [[UIImageView alloc] initWithFrame:CGRectMake(640, 0, 320, 460)];
    [imageview2 setImage:[UIImage imageNamed:@"2.png"]];
    [scrollView addSubview:imageview2];
    [imageview2 release];

    UIImageView *imageview3 = [[UIImageView alloc] initWithFrame:CGRectMake(960, 0, 320, 460)];
    [imageview3 setImage:[UIImage imageNamed:@"3.png"]];
    imageview3.userInteractionEnabled = YES; // 必須打開交互,否則下面的 button 無法響應
    [scrollView addSubview:imageview3];
    [imageview3 release];

    // 在最後一頁圖上疊一個透明 button,用來觸發「開始使用」
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(0, 0, 320, 460);
    [button addTarget:self action:@selector(firstpressed) forControlEvents:UIControlEventTouchUpInside];
    [imageview3 addSubview:button];

    [self.view addSubview:scrollView];
    [scrollView release];
}

// 點擊「開始使用」後跳轉到根視圖
- (void)firstpressed
{
    [self presentModalViewController:[[[WeiBoViewController alloc] init] autorelease] animated:YES];
}

最後一頁的圖上畫了一個「開始使用」的按鈕,由於圖片本身無法響應點擊,所以我在上面疊一個透明 button 來接收事件。

二、在 AppDelegate 中判斷是否首次啟動

#import "UserGuideViewController.h"
#import "WeiBoViewController.h"

- (BOOL)application:(UIApplication *)application
    didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];

    // 判斷是否為第一次啟動
    if (![[NSUserDefaults standardUserDefaults] boolForKey:@"firstLaunch"]) {
        [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"firstLaunch"];
        NSLog(@"第一次啟動");
        // 首次啟動:以引導頁作為根視圖
        UserGuideViewController *userGuideViewController = [[UserGuideViewController alloc] init];
        self.window.rootViewController = userGuideViewController;
        [userGuideViewController release];
    } else {
        NSLog(@"不是第一次啟動");
        // 非首次:以主視圖作為根視圖
        WeiBoViewController *weiBoViewController = [[WeiBoViewController alloc] init];
        self.window.rootViewController = weiBoViewController;
        [weiBoViewController release];
    }

    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    return YES;
}

原理小結

核心邏輯只有一句:用 NSUserDefaults 的 boolForKey:@"firstLaunch" 判斷。第一次啟動時該鍵為不存在(等於 NO),於是顯示引導頁並順手把旗標設為 YES;之後每次啟動都會走 else 分支,直接進入主視圖。簡單、可靠,也不會干擾使用者。

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