程序通常会有启动页和引导页.
启动页每一次运行程序都会有;
引导页只有在程序第一次运行时,才有.
下面说一下我做的引导页的过程
在AppDelegate里面.h文件中
@interface AppDelegate : UIResponder <UIApplicationDelegate, UIAlertViewDelegate,UIScrollViewDelegate>
{
FirstSingleData *userData;
UIScrollView * introduceView;
UIImageView * lastView;
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];//一定要在这行代码之后加引导页
//数据类
userData = [FirstSingleData shardFirst];
[userData getFirstList];
//添加引导页
[self addGuideView];
return YES;
}//引导页只执行一次
- (void)addGuideView
{
// 读取沙盒数据
NSUserDefaults * settings1 = [NSUserDefaults standardUserDefaults];
NSString *key1 = @"is_first";
NSString *value = [settings1 objectForKey:key1];
// 如果没有数据
if (!value)
{
// 加入引导页
[self addIntroView];
// 写入数据
NSUserDefaults * setting = [NSUserDefaults standardUserDefaults];
NSString * key = @"is_first";
[setting setObject:[NSString stringWithFormat:@"false"] forKey:key];
[setting synchronize];
}else{
//如果不是第一次运行程序,直接进入主页面
MyTabBarViewController * root = [[[MyTabBarViewController alloc]init]autorelease];
[root.tabBar setHidden:YES];
[self.window setRootViewController:root];
}
}
//添加引导页
- (void)addIntroView
{
//判断屏幕尺寸
float height = 0.0;
if (iPhone5) {
height = 568;
}else{
height = 480;
}
//图片路径的数组
NSMutableArray * imageArray = [userData.dataDic objectForKey:@"guide"];
introduceView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, height)];
introduceView.showsHorizontalScrollIndicator = NO;
introduceView.contentSize = CGSizeMake(320 * imageArray.count, 0);
introduceView.pagingEnabled = YES;
introduceView.bounces = NO;
introduceView.delegate = self;
[self.window addSubview:introduceView];
[introduceView release];
for (int i = 0; i < [imageArray count]; i++){
NSDictionary * dic = [imageArray objectAtIndex:i];
UIImageView * scrollImage = [[UIImageView alloc]initWithFrame:CGRectMake(320*i, 0, 320, height)];
if (height == 568) {
NSString * imagePath = [dic objectForKey:@"imgPath1"];
NSLog(@"image%d = %@",i,imagePath);
if (i == 0) {
//由于第一次运行时,启动页运行完,会出现空白一段时间,原因是因为引导页的第一张图片没加载完成,
为了让视觉效果更好,启动页结束马上看到引导页,有连贯的效果,将引导页的第一张图片的placeHoder给
跟启动页一样的图片,这样就解决了这个问题.
[scrollImage setImageWithURL:[NSURL URLWithString:[self imageAddHandle:imagePath]] placeholderImage:[UIImage imageNamed:@"[email protected]"]];
}else{
[scrollImage setImageWithURL:[NSURL URLWithString:[self imageAddHandle:imagePath]] placeholderImage:nil];
}
}else if (height == 480){
NSString * imagePath = [dic objectForKey:@"imgPath2"];
if (i == 0) {
[scrollImage setImageWithURL:[NSURL URLWithString:[self imageAddHandle:imagePath]] placeholderImage:[UIImage imageNamed:@"Default@2x"]];
}else{
[scrollImage setImageWithURL:[NSURL URLWithString:[self imageAddHandle:imagePath]] placeholderImage:nil];
}
}
scrollImage.tag = 300 + i;
[introduceView addSubview:scrollImage];
[scrollImage release];
}
//获取最后一张图片
lastView = (UIImageView *)[introduceView viewWithTag:imageArray.count - 1 + 300];
lastView.userInteractionEnabled = YES;
//给最后一张图片添加点击进入的手势
UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] init];
[tapGestureRecognizer addTarget:self action:@selector(gestureRecognizerHandle:)];
[tapGestureRecognizer setNumberOfTapsRequired:1];
[tapGestureRecognizer setNumberOfTouchesRequired:1];
[lastView addGestureRecognizer:tapGestureRecognizer];
[tapGestureRecognizer release];
}
- (void)gestureRecognizerHandle:(UITapGestureRecognizer *)gesture{
//移除引导页
[introduceView removeFromSuperview];
//进入主页面
MyTabBarViewController * root = [[[MyTabBarViewController alloc]init]autorelease];
[root.tabBar setHidden:YES];
[self.window setRootViewController:root];
}
//对图片的路径进行处理
- (NSString *)imageAddHandle:(NSString *)aString {
NSString *tempBgSkinPicUrlLString = [aString substringFromIndex:9];
NSString *tempNewBgSkinPicUrlLString = [SERVERADD stringByAppendingString:tempBgSkinPicUrlLString];
return tempNewBgSkinPicUrlLString;
}