這個功能的重點就是在如何判斷應用是第一次啟動的. 其實很簡單
我們只需要在一個類里面寫好用戶引導頁面 基本上都是使用UIScrollView 來實現,
新建一個繼承於UIViewController的類 命名為 UserGuideViewController ,
在UserGuideViewController.m 寫
1 - (void)viewDidLoad 2 { 3 [super viewDidLoad]; 4 // Do any additional setup after loading the view. 5 self.view.backgroundColor = [UIColor redColor]; 6 7 [self initGuide]; //加載新用戶指導頁面 8 } 9 10 - (void)initGuide 11 { 12 UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 640)]; 13 [scrollView setContentSize:CGSizeMake(1280, 0)]; 14 [scrollView setPagingEnabled:YES]; //視圖整頁顯示 15 // [scrollView setBounces:NO]; //避免彈跳效果,避免把根視圖露出來 16 17 UIImageView *imageview = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 460)]; 18 [imageview setImage:[UIImage imageNamed:@"0.png"]]; 19 [scrollView addSubview:imageview]; 20 [imageview release]; 21 22 UIImageView *imageview1 = [[UIImageView alloc] initWithFrame:CGRectMake(320, 0, 320, 460)]; 23 [imageview1 setImage:[UIImage imageNamed:@"1.png"]]; 24 [scrollView addSubview:imageview1]; 25 [imageview1 release]; 26 27 UIImageView *imageview2 = [[UIImageView alloc] initWithFrame:CGRectMake(640, 0, 320, 460)]; 28 [imageview2 setImage:[UIImage imageNamed:@"2.png"]]; 29 [scrollView addSubview:imageview2]; 30 [imageview2 release]; 31 32 UIImageView *imageview3 = [[UIImageView alloc] initWithFrame:CGRectMake(960, 0, 320, 460)]; 33 [imageview3 setImage:[UIImage imageNamed:@"3.png"]]; 34 imageview3.userInteractionEnabled = YES; //打開imageview3的用戶交互;否則下面的button無法響應 35 [scrollView addSubview:imageview3]; 36 [imageview3 release]; 37 38 UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];//在imageview3上加載一個透明的button 39 [button setTitle:nil forState:UIControlStateNormal]; 40 [button setFrame:CGRectMake(46, 371, 230, 37)]; 41 [button addTarget:self action:@selector(firstpressed) forControlEvents:UIControlEventTouchUpInside]; 42 [imageview3 addSubview:button]; 43 44 [self.view addSubview:scrollView]; 45 [scrollView release]; 46 }
button的方法
1 - (void)firstpressed 2 { 3 [self presentModalViewController:[[[WeiBoViewController alloc] init] autorelease] animated:YES]; //點擊button跳轉到根視圖 4 }
至於添加button是因為我的用戶引導最后一個頁面有一個畫上去的button,寫着 開始使用 我在上面添加一個透明的button 用以實現調用方法
打開AppDelegate.m
首先引入頭文件
1 #import "UserGuideViewController.h" 2 #import "WeiBoViewController.h"
WeiBoViewController是我的根視圖
application: didFinishLaunchingWithOptions: 方法內進行判斷
1 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 2 { 3 self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease]; 4 // Override point for customization after application launch. 5 6 //判斷是不是第一次啟動應用 7 if(![[NSUserDefaults standardUserDefaults] boolForKey:@"firstLaunch"]) 8 { 9 [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"firstLaunch"]; 10 NSLog(@"第一次啟動"); 11 //如果是第一次啟動的話,使用UserGuideViewController (用戶引導頁面) 作為根視圖 12 UserGuideViewController *userGuideViewController = [[UserGuideViewController alloc] init]; 13 self.window.rootViewController = userGuideViewController; 14 [userGuideViewController release]; 15 } 16 else 17 { 18 NSLog(@"不是第一次啟動"); 19 //如果不是第一次啟動的話,使用LoginViewController作為根視圖 20 WeiBoViewController *weiBoViewController = [[WeiBoViewController alloc] init]; 21 self.window.rootViewController = weiBoViewController; 22 [weiBoViewController release]; 23 24 } 25 26 self.window.backgroundColor = [UIColor whiteColor]; 27 [self.window makeKeyAndVisible]; 28 return YES; 29 }
這樣,就可以實現第一次打開應用顯示用戶引導 之后不再顯示