1. 程式人生 > >APP首次啟動引導介面和啟動介面設定——iOS開發

APP首次啟動引導介面和啟動介面設定——iOS開發

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; }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18

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;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29

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];
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

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;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37

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

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

主介面: 
主介面