本章简述了IOS开发过程中程序第一次启动时的程序引导的示例,主要用到了UIScrollView作引导界面,使用NSUserDefaults相关键值判断程序是否第一次启动。
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // Override point for customization after application launch. // 是否第一次启动 if(![[NSUserDefaults standardUserDefaults] boolForKey:@"firstLaunch"]) { // 注意设置为TRUE [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"everLaunched"]; [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"firstLaunch"]; ViewController* viewController = [[ViewController alloc] init]; viewController.firsttime = YES; self.window.rootViewController = viewController; } else { NSLog(@"不是第一次启动"); } return YES; }此处注意,判断第一成功后把相关键值的值设置成了YES,使得下次不会再判断为YES,网上好多人都没有设置,但是我没设置就老是判断为是第一次启动,不知道其他人是什么情况。
同时,因为启动页面我是把他作为子页面放在程序的主页面里的,所以这里通过主页面控制器的属性来判断要不要显示引导页面,此处在判断为第一次启动后,将属性firsttime设置为YES,然后程序就会显示引导页面。
- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. [self initUI]; } - (void)initUI { // 启动动画 if(self.firsttime) { [self showStartPage]; self.firsttime = NO; } else { self.view.backgroundColor = [UIColor whiteColor]; UILabel* label = [[UILabel alloc] initWithFrame:CGRectMake(20, 200, 350, 40)]; label.text = @"欢迎你来到我的应用,我们在完善中..."; [self.view addSubview:label]; } } - (void)showStartPage { NSInteger pageNumber = 3; // 滚动页 UIScrollView* scrollView = [[UIScrollView alloc] initWithFrame:self.view.frame]; [scrollView setContentSize:CGSizeMake(scrollView.frame.size.width*pageNumber, 0)]; scrollView.pagingEnabled = YES; [self.view addSubview:scrollView]; _scrollView = scrollView; CGRect frame = self.view.frame; frame.origin.x -= frame.size.width; for (NSInteger i=0; i<pageNumber; i++) { frame.origin.x += frame.size.width; UIImageView* imageView = [[UIImageView alloc] initWithFrame:frame]; [imageView setImage:[UIImage imageNamed:[NSString stringWithFormat:@"%ld.jpg", i+1]]]; //[imageView setContentMode:UIViewContentModeScaleAspectFill]; [scrollView addSubview:imageView]; } CGFloat w = frame.size.width/2; CGFloat h = 40; CGFloat x = frame.origin.x + (frame.size.width-w)/2; CGFloat y = frame.origin.y+frame.size.height-60; UIButton* welcomeButton = [[UIButton alloc] initWithFrame:CGRectMake(x, y, w, h)]; [welcomeButton setTitle:@"进入查看更多精彩" forState:UIControlStateNormal]; [scrollView addSubview:welcomeButton]; [welcomeButton addTarget:self action:@selector(onWelcome:) forControlEvents:UIControlEventTouchUpInside]; } - (void)onWelcome:(id)sender { for (UIView* view in [_scrollView subviews]) { [view removeFromSuperview]; } [_scrollView removeFromSuperview]; [self initUI]; }当外面设置了firsttime为YES时,会显示引导页面,此处将三幅图片作为引导页面示意的,使用控件UIScrollView管理。同时最后一个页面添加了一个按钮,点击该按钮即可进入程序主界面。
原文地址:http://blog.csdn.net/arbboter/article/details/44046171