標籤:ios開發 啟動動畫 程式引導
摘要
本章簡述了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管理。同時最後一個頁面添加了一個按鈕,點擊該按鈕即可進入程式主介面。
運行結果
IOS程式啟動引導樣本