1:创建一个UserGuideViewController类,继承于 UIViewController。其.m文件代码如下:
#import "UserGuideViewController.h"
#import "ViewController.h"
//用宏定义获取屏幕的宽和高
#define MAINSCREEN_WIDTH ([[UIScreen mainScreen] bounds].size.width)
#define MAINSCREEN_HETGHT ([[UIScreen mainScreen] bounds].size.height)
@interface UserGuideViewController ()<UIScrollViewDelegate>
@property (nonatomic ,strong) UIScrollView *scrollView;
@property (nonatomic ,strong) UIPageControl *pageCtl;
@end
@implementation UserGuideViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
//创建UIScrollView对象
self.scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, MAINSCREEN_WIDTH, MAINSCREEN_HETGHT)];
//设置代理
self.scrollView.delegate = self;
//设置UIScrollView对象的内容区域
self.scrollView.contentSize = CGSizeMake(MAINSCREEN_WIDTH *4, MAINSCREEN_HETGHT);
//设置UIScrollView翻页功能
self.scrollView.pagingEnabled = YES;
//设置UIScrollView的弹簧效果
self.scrollView.bounces = NO ;
//关闭滚动条
self.scrollView.showsHorizontalScrollIndicator = NO;
self.scrollView.showsVerticalScrollIndicator = NO;
[self.view addSubview:self.scrollView];
//在 scrollView 上添加4个 imageView 页面,用于用户指导
for (NSInteger i = 0; i < 4; i++) {
//创建image 对象
UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"sence%d.jpg",i+1]];
//创建 imageView 对象
UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0 +MAINSCREEN_WIDTH * i, 0, MAINSCREEN_WIDTH, MAINSCREEN_HETGHT)];
//设置imageView 对象的image 属性
imageView.image = image;
//为第4页开启用户交互功能,并创建开始按钮
if (i == 3) {
imageView.userInteractionEnabled = YES;
//创建开始按钮
UIButton *goBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
goBtn.frame = CGRectMake(MAINSCREEN_WIDTH-60, MAINSCREEN_HETGHT-60, 60, 30);
[goBtn setTitle:@"开始体验" forState:UIControlStateNormal];
[goBtn setTitleColor:[UIColor purpleColor] forState:UIControlStateNormal];
[goBtn addTarget:self action:@selector(goForward:) forControlEvents:UIControlEventTouchUpInside];
[imageView addSubview:goBtn];
}
[self.scrollView addSubview:imageView];
}
//创建pageCtl
self.pageCtl = [[UIPageControl alloc]initWithFrame:CGRectMake(MAINSCREEN_WIDTH/2-40, MAINSCREEN_HETGHT-30, 80, 30)];
//设置页数
self.pageCtl.numberOfPages = 4;
//设置当前页码
self.pageCtl.currentPage = 0;
//设置当前页及其他未选择页面页码显示器显示的颜色
self.pageCtl.currentPageIndicatorTintColor =[UIColor redColor];
self.pageCtl.pageIndicatorTintColor = [UIColor whiteColor];
[self.view addSubview:self.pageCtl];
}
#pragma mark goForward
- (void)goForward:(UIButton *)sender{
ViewController *VC = [[ViewController alloc]init];
[self presentViewController:VC animated:YES completion:^{
NSLog(@"从用户指导页面进入内容页面");
}];
}
#pragma mark UIScrollViewDelegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
self.pageCtl.currentPage = scrollView.contentOffset.x / MAINSCREEN_WIDTH;
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
self.window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];
//判断程序是否是第一次启动 (重点在此)
if (![[NSUserDefaults standardUserDefaults] boolForKey:@"everLaunched"]) {
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"everLaunched"];
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"firstLaunched"];
}else{
[[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"firstLaunched"];
}
//根据 key->firstLaunched的值,使用不同的视图作为根视图
//self.firstLaunched 为代理类的属性成员。
self.firstLaunched = [[NSUserDefaults standardUserDefaults] boolForKey:@"firstLaunched"];
if (self.firstLaunched) {
UserGuideViewController *userGuideVC = [[UserGuideViewController alloc]init];
self.window.rootViewController = userGuideVC;
}else{
ViewController *VC = [[ViewController alloc]init];
self.window.rootViewController = VC;
}
self.window.backgroundColor =[UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
}