应用开机引导页并不是每次运行都显示的;首先需要判断应用是否是安装后首次运行或者升级后首次运行。
需要用到一个方法:
获取应用版本号:key值:CFBundleShortVersionString,也有人使用通过获取应用的build版本号判断的,这里我使用的是前者
定义一个实例方法,在方法中:
获取应用版本号:
NSString *currentVersion = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"];//获取应用版本号
定义一个标识:
NSString *key = @"APP_LASTLANUCH_VERSION";
首先判断这个标识是否有值:
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString *lastLaunchVersion = [defaults objectForKey:key];
判断:
if (lastLaunchVersion == nil) {
//第一次运行
[defaults setObject:currentVersion forKey:key];
return YES;
}else if (![lastLaunchVersion isEqualToString:currentVersion]){
//升级后第一次
[defaults setObject:currentVersion forKey:key];
return YES;
}
其他结果均返回NO
在AppDelegate的
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
BOOL isFirst = [self isFirstLoad];
if (isFirst) {
GuideView *guideView = [[GuideView alloc] initWithFrame:[UIScreen mainScreen].bounds];
[self.window.rootViewController.view addSubview:guideView];
}
return YES;
}
方法中调用之前定义的那个实例方法.
新建一个添加引导页内容的GuideView类:
初始化:
- (instancetype)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
[self createView];
}
return self;
}
定义的createView方法:
NSArray *imagesArray = @[@"guide1", @"guide2", @"guide3", @"guide4", @"guide5", @"guide6"];//我从网上随便找了几张图 并不是我项目中的原图。项目中 存在“登录/注册”和“立即体验”两个按钮,这里只有最后一张图有个“Go”按钮
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:self.bounds];
[self addSubview:scrollView];
//设置是否有橡皮筋效果
scrollView.bounces = NO;//默认YES
//设置是否使用分页属性
scrollView.pagingEnabled = YES;//默认NO
//设置是否允许滚动属性
scrollView.scrollEnabled = YES;//默认为YES
//设置是否显示水平、竖直滚动条属性 默认均为YES
scrollView.showsHorizontalScrollIndicator = NO;
scrollView.showsVerticalScrollIndicator = NO;
scrollView.delegate = self;//设置代理
//设置内容范围属性
scrollView.contentSize = CGSizeMake(self.frame.size.width * (imagesArray.count + 1), self.frame.size.height);
//为每一页添加数组数据
for (int i = 0; i < imagesArray.count; i ++) {
UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(i * scrollView.frame.size.width, 0, scrollView.frame.size.width, scrollView.frame.size.height)];
imgView.tag = 2016 + i;
imgView.userInteractionEnabled = YES;
imgView.image = [UIImage imageNamed:imagesArray[i]];
[scrollView addSubview:imgView];
}
scrollView代理方法:
#pragma mark -UIScrollViewDelegate
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
NSInteger pageIndex = scrollView.contentOffset.x / scrollView.frame.size.width;
if (pageIndex == 5) {
if (_btn == nil) {
_btn = [UIButton buttonWithType:UIButtonTypeCustom];
_btn.backgroundColor = [UIColor clearColor];
[_btn addTarget:self action:@selector(btnClickAction) forControlEvents:UIControlEventTouchUpInside];
UIImageView *imgView = (UIImageView *)[scrollView viewWithTag:2021];
[imgView addSubview:_btn];
//这里不一定正好遮住Go按钮哈 -_- 偷懒
__weak typeof(self) weakSelf = self;
[_btn mas_makeConstraints:^(MASConstraintMaker *make) {
make.leading.equalTo(weakSelf.mas_centerX).offset(-75);
make.bottom.mas_equalTo(-60);
make.width.mas_equalTo(160);
make.height.mas_equalTo(60);
}];
}
}else {
if (_btn) {
[_btn removeFromSuperview];
_btn = nil;
}
}
if (pageIndex < 6) {
//
}else {
[self performSelector:@selector(removeFromSuperview) withObject:nil afterDelay:0.5];
}
}
移除方法:
- (void)btnClickAction
{
[self performSelector:@selector(removeFromSuperview) withObject:nil];
}