iOS快速集成启动广告页(欢迎页)
目前市场上很多APP(如淘宝、美团、微博、UC),都会在第一次使用的时候 有欢迎页,之后再次使用就没有欢迎页了,如果没做过的话,总是感觉特别复杂,做了才知道真的很简单,下面是我做的例子:
- 先看gif效果图
具体步骤如下
1、AppDelegate.m 里面 导入
#import "ViewController.h"
#import "VistaWelcomeViewController.h"
接着写如下代码 方法:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
//初始化
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
//判断是不是第一次 数据持久化到本地 存储沙河
BOOL isWel = [[NSUserDefaults standardUserDefaults]objectForKey:@"isWelcomeDone"];
if (isWel) {//有这个值,不是第一次进入,直接进入首页
ViewController *VC=[[ViewController alloc]init];
self.window.rootViewController = VC;
}else{//没有,第一次引用,进入引导页
VistaWelcomeViewController *vc = [[VistaWelcomeViewController alloc]init];
UINavigationController *naVC = [[UINavigationController alloc]initWithRootViewController:vc];
self.window.rootViewController = naVC;
}
return YES;
}
2、在VistaWelcomeViewController.m文件中写
//初始化UI
-(void)initWelcomeView{
//初始化滚动视图
self.scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0,self.view.frame.size.width, self.view.frame.size.height)];
self.scrollView.backgroundColor = [UIColor whiteColor];
[self.scrollView setContentSize:CGSizeMake(self.view.frame.size.width * 3, 0)];
self.scrollView.pagingEnabled = YES;
self.scrollView.scrollEnabled = YES;
self.scrollView.delegate = self;
self.scrollView.bounces = NO;
self.scrollView.showsVerticalScrollIndicator = NO;
self.scrollView.showsHorizontalScrollIndicator = NO;
if (@available(iOS 11.0, *)) {
self.scrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
}else {
self.automaticallyAdjustsScrollViewInsets = NO;
}
[self.view addSubview:self.scrollView];
for (int i = 0; i < 3; i ++) {
UIImageView *imageVie = [[UIImageView alloc]initWithFrame:CGRectMake(self.view.frame.size.width * i, 0, self.view.frame.size.width, self.view.frame.size.height)];
imageVie.contentMode = UIViewContentModeScaleToFill;
imageVie.userInteractionEnabled = YES;
imageVie.image = [UIImage imageNamed:[NSString stringWithFormat:@"welcome_0%d",i+1]];
[self.scrollView addSubview:imageVie];
}
//最后一页的按钮
UIButton *experienceBtn=[UIButton buttonWithType:UIButtonTypeCustom];
experienceBtn.frame=CGRectMake(self.view.frame.size.width *2+(_scrollView.frame.size.width-200)/2,_scrollView.frame.size.height-90, 200, 40);
[_scrollView addSubview:experienceBtn];
[experienceBtn setBackgroundImage:[UIImage imageNamed:@"anniu"] forState:0];
[experienceBtn addTarget:self action:@selector(beginLearning) forControlEvents:UIControlEventTouchUpInside];
//滚动指示点
_pageControl = [[UIPageControl alloc]init];
[_pageControl setNumberOfPages:3];
[_pageControl setPageIndicatorTintColor:[UIColor lightGrayColor]];
[_pageControl setCurrentPageIndicatorTintColor:[UIColor redColor]];
[self.view addSubview:_pageControl];
[_pageControl mas_makeConstraints:^(MASConstraintMaker *make) {
make.bottom.mas_equalTo(-5);
make.centerX.mas_equalTo(0);
make.width.mas_equalTo(100);
}];
}
//UIScrollView的代理方法,实现UIPageController的值改变
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
NSInteger index=scrollView.contentOffset.x/scrollView.bounds.size.width;
[_pageControl setCurrentPage:index];
}
#pragma mark-点击进入app首页
-(void)beginLearning{
//数据持久化,再次使用的时候,不再有欢迎页
[[NSUserDefaults standardUserDefaults]setBool:YES forKey:@"isWelcomeDone"];
[[NSUserDefaults standardUserDefaults]synchronize];
ViewController *vc = [ViewController new];
UINavigationController *nvc = [[UINavigationController alloc]initWithRootViewController:vc];
[self presentViewController:nvc animated:YES completion:nil];
}
这样就可以了
demo