在AppDelegate.m中:
我们需要两个Viewcongtroller来实现;
myViewController是我的引导页面视图控制器
MainViewController是我们滑动完引导页 点击按钮以后进入的主页面。
#import "AppDelegate.h"
#import "myViewController.h"
#import "MainViewController.h"
@interface AppDelegate ()
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
NSUserDefaults *useDef = [NSUserDefaults standardUserDefaults];
// 使用 NSUserDefaults 读取用户数据
if (![useDef boolForKey:@"notFirst"]) {
// 如果是第一次进入引导页
_window.rootViewController = [[myViewController alloc] init];
}
else{
// 否则直接进入应用
_window.rootViewController = [[MainViewController alloc] init];
}
return YES;
}
myViewController.m中:
#import "myViewController.h"
#import "MainViewController.h"
#define WIDTH (NSInteger)self.view.bounds.size.width
#define HEIGHT (NSInteger)self.view.bounds.size.height
@interface myViewController ()
{
// 创建页码控制器
UIPageControl *pageControl;
// 判断是否是第一次进入应用
BOOL flag;
}
@end
@implementation myViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
UIScrollView *myScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT)];
for (int i=0; i<3; i++) {
UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"Y%d.jpg",i+1]];
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(WIDTH * i, 0, WIDTH, HEIGHT)];
// 在最后一页创建按钮
if (i == 2) {
// 必须设置用户交互 否则按键无法操作
imageView.userInteractionEnabled = YES;
UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
button.frame = CGRectMake(WIDTH / 3, HEIGHT * 7 / 8, WIDTH / 3, HEIGHT / 16);
[button setTitle:@"点击进入" forState:UIControlStateNormal];
[button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
button.layer.borderWidth = 2;
button.layer.cornerRadius = 5;
button.clipsToBounds = YES;
button.layer.borderColor = [UIColor whiteColor].CGColor;
[button addTarget:self action:@selector(go:) forControlEvents:UIControlEventTouchUpInside];
[imageView addSubview:button];
}
imageView.image = image;
[myScrollView addSubview:imageView];
}
myScrollView.bounces = NO;
myScrollView.pagingEnabled = YES;
myScrollView.showsHorizontalScrollIndicator = NO;
myScrollView.contentSize = CGSizeMake(WIDTH * 3, HEIGHT);
myScrollView.delegate = self;
[self.view addSubview:myScrollView];
pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(WIDTH / 3, HEIGHT * 15 / 16, WIDTH / 3, HEIGHT / 16)];
// 设置页数
pageControl.numberOfPages = 3;
// 设置页码的点的颜色
pageControl.pageIndicatorTintColor = [UIColor yellowColor];
// 设置当前页码的点颜色
pageControl.currentPageIndicatorTintColor = [UIColor redColor];
[self.view addSubview:pageControl];
}
#pragma mark - UIScrollViewDelegate
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
// 计算当前在第几页
pageControl.currentPage = (NSInteger)(scrollView.contentOffset.x / [UIScreen mainScreen].bounds.size.width);
}
// 点击按钮保存数据并切换根视图控制器
- (void) go:(UIButton *)sender{
flag = YES;
NSUserDefaults *useDef = [NSUserDefaults standardUserDefaults];
// 保存用户数据
[useDef setBool:flag forKey:@"notFirst"];
[useDef synchronize];
// 切换根视图控制器
self.view.window.rootViewController = [[MainViewController alloc] init];
}