Tag: ios development interface start

Tags: ios development    interface start   


APP download and install for the first time will generally display a first boot guide interface and then enter the main interface, non-first start APP will usually display a boot interface and then enter the main interface.


1. In this example, start to display FirstUseViewController for the first time, add a button, and click to enter LaunchViewController 
2. It is not the first LaunchViewController, and enter the main interface ViewController after displaying 2s. 
3. The main interface ViewController 
4. Don't go into details, generally there will be animations, pictures, etc. Yes, it is not the focus of this exercise, so there is no setting, only a simple mark for interface distinction 
(the effect picture is at the end of the text)


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 settings, two methods. I personally feel that the second method is more convenient to use 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;
}


Guess you like

Origin blog.csdn.net/qq_27740983/article/details/51392825