APP首次启动引导界面和启动界面设置——iOS开发

本文介绍了一种iOS应用中常见的启动界面设计方法。通过不同视图控制器的切换实现了首次使用引导与常规启动流程的区别处理,并提供了简洁的界面切换动画。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 
标签: iOS开发界面启动
 分类:
UI基础(19) 

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

界面效果图: 
首次启动页面: 
首次启动页面


非首次启动页面: 
非首次启动页面


主界面: 
主界面

 

转载于:https://www.cnblogs.com/ios4app/p/6873571.html

### iOS Objective-C 设置启动页的方法 在iOS开发中,启动页(Launch Screen)是一个重要的用户体验部分。它通常用于展示应用的品牌形象或作为加载数据时的一个过渡界面。以下是通过Objective-C实现设置启动页的具体方法。 #### 创建 LaunchScreen.storyboard 文件 Xcode 提供了一种简便的方式来创建启动页——使用 `LaunchScreen.storyboard` 文件。该文件允许开发者设计一个静态的启动页面,在应用程序启动期间显示给用户[^1]。 如果项目尚未包含此文件,则可以通过以下方式添加: 1. 打开 Xcode 并导航到项目的根目录。 2. 右键单击左侧的 **Project Navigator** 面板并选择 “New File…”。 3. 选择模板中的 **User Interface -> Storyboard**,命名为 `LaunchScreen.storyboard`。 #### 设计启动页布局 打开 `LaunchScreen.storyboard` 后,可以拖拽 UI 组件来构建所需的启动页样式。常见的组件包括图像视图 (`UIImageView`) 和标签 (`UILabel`) 等。这些控件应放置于屏幕中央或其他固定位置以适应不同的设备尺寸。 为了确保启动页能在各种 iPhone 屏幕大小上正常工作,请启用自动布局功能 (**Auto Layout**) 来约束各个元素的位置关系。例如: ```objc // 示例代码片段:配置 UIImageView 的 AutoLayout Constraints UIView *imageView = [[UIImageView alloc] initWithFrame:CGRectZero]; [self.view addSubview:imageView]; NSLayoutConstraint *centerXConstraint = [NSLayoutConstraint constraintWithItem:imageView attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterX multiplier:1 constant:0]; NSLayoutConstraint *centerYConstraint = [NSLayoutConstraint constraintWithItem:imageView attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterY multiplier:1 constant:0]; [imageView.widthAnchor constraintEqualToConstant:100].active = YES; [imageView.heightAnchor constraintEqualToConstant:100].active = YES; [self.view addConstraints:@[centerXConstraint, centerYConstraint]]; ``` 上述代码展示了如何利用 NSLayoutConstraint 将图片居中定位,并设定其宽高属性。 #### 更新 Info.plist 中的相关字段 最后一步是告知系统哪个 storyboard 应被用作启动画面。这可通过修改工程内的 `Info.plist` 文件完成。确认存在如下两个键值对项: - `UILaunchStoryboardName`: 值设为 `LaunchScreen` (即刚才新建的故事版名称) 这样当 App 运行时就会先显示出所定义好的启动屏效果直到主界面准备完毕为止。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值