Etiqueta: inicio de la interfaz de desarrollo de ios

Etiquetas: inicio de la interfaz de desarrollo de ios      


La descarga e instalación de la APLICACIÓN por primera vez generalmente mostrará una primera interfaz de guía de arranque y luego ingresará a la interfaz principal, la APLICACIÓN que no sea el primer inicio generalmente mostrará una interfaz de arranque y luego ingresará a la interfaz principal.


1. En este ejemplo, comience a mostrar FirstUseViewController por primera vez, agregue un botón y haga clic para ingresar a LaunchViewController. 
2. No es el primer LaunchViewController, e ingrese a la interfaz principal ViewController después de mostrar 2s. 
3. La interfaz principal ViewController 
4. No entre en detalles. Generalmente, habrá animaciones, imágenes, etc. Sí, no es el enfoque de este ejercicio, por lo que no hay configuración, solo una simple marca para la distinción de la interfaz 
(la imagen del efecto está al final del texto)


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];
}

Configuración de AppDelegate.m, dos métodos. Personalmente, creo que el segundo método es más conveniente para usar 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;
}


Supongo que te gusta

Origin blog.csdn.net/qq_27740983/article/details/51392825
Recomendado
Clasificación