APP首次启动引导界面和启动界面设置——iOS开发
 分类:
 分类:版权声明:本文为博主原创文章,转载请注明出处http://blog.csdn.net/zsk_zane
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
- 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
- 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
- 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
- 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
界面效果图: 
首次启动页面: 
非首次启动页面: 
主界面: 
 
                    
                     
                    
                 
                    
                
 
 
                
            
         
         浙公网安备 33010602011771号
浙公网安备 33010602011771号