1.每个应用程序刚安装后启动的时候都会有一个引导页,用于引导用户使用APP,怎么实现呢,首先来把引导页写好,说白了,它就是一个scrollview,UIPageControl,按钮或者手势(进入主界面), 这里我用的是手势点击进入主界面,(当然 也你可以添加按钮)
/**
* 创建基础控件
*/
-(void)creatWelcomeView
{
/*
添加滚动视图
*/
_scrollview = [[UIScrollView alloc]initWithFrame:screen];
// _scrollview.backgroundColor = [UIColor greenColor];
_scrollview.pagingEnabled = YES;
_scrollview.contentSize = CGSizeMake(WIDTH*_images.count, HEIGHT);
_scrollview.delegate = self;
[self.view addSubview:_scrollview];
for (int i =0; i<_images.count; i++) {
_imageview = [[UIImageView alloc]initWithImage:[UIImage imageNamed:[_images objectAtIndex:i]]];
// NSLog(@"%@",[NSString stringWithFormat:@"%d.jpeg",i]);
_imageview.frame = CGRectMake(i*WIDTH, 0, WIDTH, HEIGHT);
_imageview.userInteractionEnabled = YES;
[_imageview setTag:100+i];
[_scrollview addSubview:_imageview];
[_imageViews addObject:_imageview];
}
/*
添加分页控制
*/
_pageControl = [[UIPageControl alloc]initWithFrame:CGRectMake(0, HEIGHT-80, WIDTH, WIDTH/4)];
_pageControl.backgroundColor = [UIColor clearColor];
_pageControl.numberOfPages = _images.count;
_pageControl.tintColor = [UIColor colorWithWhite:255.0/254 alpha:1.0];
_pageControl.currentPageIndicatorTintColor= [UIColor colorWithWhite:255.0/250 alpha:0.8];
[_pageControl addTarget:self action:@selector(pageControlClicked) forControlEvents:UIControlEventValueChanged];
[self.view addSubview:_pageControl];
/**
*可以在Imageview上添加你要的处理的事件,通过在imageview添加button 或者 手势 来处理事件
*/
/*
*这里我为最后一张图片添加点击手势 (进入下个视图控制器)
*/
UITapGestureRecognizer *tapGo = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapdo:)];
/*
遍历获取最后一个imageview
*/
for (UIImageView *view in _imageViews) {
if (view.tag-100 == _images.count - 1) {
[view addGestureRecognizer:tapGo];
}
}
}
2.接下来就是关键了,就是怎么来判断这个程序是不是刚安装呢,在这里我用了NSUserDefaults这个类,它会将数据存到应用里,
NSUserDefaults 可以存储数据类型(CGflot,NSInteger,BOOL等)和对象(NSData,NSArray,NSString,NSDictionary 等),我用了一个BOOL值来记录第一次运行,
所以我在Appdelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
/**
* NSUserDefaults 可以存储数据类型(CGflot,NSInteger,BOOL等)和对象(NSData,NSArray,NSString,NSDictionary 等)
* 这里利用NSUserDefaults 设置一个bool值来判断是不是第一次运行
*/
if (![[NSUserDefaults standardUserDefaults] boolForKey:@"FirstRun"]) {
//如果是第一次运行就添加BOOL并赋值
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"FirstRun"];
}else{
}
return YES;
}
3.效果图
4.是不是很简单啊,各位,详细代码已上传github,做了简单的封装,只需调用一句话传入图片数组即可实现引导页,[github网址](https://github.com/zlfyuan/WelComeView.git )