APP首次啟動引導界面和啟動界面設置——iOS開發 – iPhone手機開發 iPhone軟體開發教學課程


APP下載安裝第一次使用一般會顯示一個首次啟動引導界面然後進入主界面,非首次開啟APP也通常會顯示一個啟動界面然後進入主界面。


1、本例首次啟動顯示FirstUseViewController,添加一個button,點擊進入LaunchViewController
2、非首次LaunchViewController,顯示2s後進入主界面ViewController
3、主界面ViewController
4、不深究細節,一般啟動引導都會有動畫,圖片之類的,非本次練習重點,所以沒有設置,隻有簡單地標志作界面區分
(效果圖在文末)


FirstUseViewController.m


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

    UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
    button.center = self.view.center;
    [button setTitle:@Welcome forState:UIControlStateNormal];
    [button addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];
}

//點擊button切換到下一個界面
- (void)btnAction:(UIButton *)btn {
    LaunchViewController *vc = [[LaunchViewController alloc] init];
    self.view.window.rootViewController = vc;
}

LaunchViewController.m

- (void)viewDidLoad {
    [super viewDidLoad];

    self.view.backgroundColor = [UIColor blueColor];
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 200, 50)];
    label.center = self.view.center;
    [label setFont:[UIFont systemFontOfSize:30]];
    label.text = @啟動頁面;
    [self.view addSubview:label];

//    延遲2s調用,一般啟動頁面會停留,或者有些動畫什麼的,本例隻簡述思路,不深究細節
    [self performSelector:@selector(changeView) withObject:self afterDelay:2];
    // Do any additional setup after loading the view.
}

//切換到下一個界面
- (void)changeView {
    UIWindow *window = self.view.window;
    ViewController *main = [[ViewController alloc] init];

    //添加一個縮放效果
    main.view.transform = CGAffineTransformMakeScale(0.2, 0.2);
    [UIView animateWithDuration:0.1 animations:^{
        main.view.transform = CGAffineTransformIdentity;
    }];

    window.rootViewController = main;
}

ViewController.m

- (void)viewDidLoad {
    [super viewDidLoad];

    self.view.backgroundColor = [UIColor grayColor];
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 50)];
    label.center = self.view.center;
    [label setFont:[UIFont systemFontOfSize:30]];
    label.text = @主界面;
    [self.view addSubview:label];
}

AppDelegate.m設置,兩種方法。個人覺得第二種利用NSUserDefaults實現更方便

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

////    利用文件操作判斷是否為第一次使用此APP
//    NSString *filePath = [NSHomeDirectory() stringByAppendingPathComponent:@Documents/firstUse.plist];    //第一次啟動,沒有此文件,會自動創建
//    NSDictionary *dic = [NSDictionary dictionaryWithContentsOfFile:filePath];
//    
//    BOOL notFirstUse = YES;
//    notFirstUse = [dic[@notFirstUse] boolValue];
//    if (!notFirstUse) {
//        NSDictionary *dic = @{@notFirstUse : @YES };
//        [dic writeToFile:filePath atomically:YES];
//        FirstUseViewController *vc = [[FirstUseViewController alloc] init];
//        self.window.rootViewController = vc;
//    }else {
//        LaunchViewController *vc = [[LaunchViewController alloc] init];
//        self.window.rootViewController = vc;
//    }
//

//    利用NSUserDefaults實現
    if(![[NSUserDefaults standardUserDefaults] boolForKey:@firstLaunch]) {
        [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@firstLaunch];
        NSLog(@首次啟動);
        FirstUseViewController *vc = [[FirstUseViewController alloc] init];
        self.window.rootViewController = vc;
    }else {
        NSLog(@非首次啟動);
        LaunchViewController *vc = [[LaunchViewController alloc] init];
        self.window.rootViewController = vc;
    }

    return YES;
}

界面效果圖:
首次啟動頁面:
首次啟動頁面


非首次啟動頁面:
非首次啟動頁面


主界面:
主界面

 

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *