我们在首次安装一个软件时都有用户引导,这里就做了一个用户引导的demo
#import "DemoViewController.h"
#import "MainViewController.h"
#define kScreenWidth [[UIScreen mainScreen]bounds].size.width
#define kScreenHeigth [[UIScreen mainScreen]bounds].size.height
#define kImageCount 4
#define kImageName @"information"
#define kScrollViewTag 101
#define kPageControlTag 102
@interface DemoViewController () <UIScrollViewDelegate>
@end
@implementation DemoViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
//布局子视图
[self layoutSubviews];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
if ([self isViewLoaded] && !self.view.window) {
self.view = nil;
}
}
- (void)layoutSubviews {
[self setupScrollView];//
[self setUpPageControl];//创建页面控制
}
- (void)setupScrollView {
UIScrollView *scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeigth)];
//整屏滑动
scroll.pagingEnabled = YES;
//关闭滚动指示条 (水平方向)
scroll.showsHorizontalScrollIndicator = NO;
//设置容量大小
scroll.contentSize = CGSizeMake(kScreenWidth *kImageCount, kScreenHeigth);//这个属性必须设定,不然不会滑动
scroll.tag = kScrollViewTag;
//设置代理
scroll.delegate = self;
[self.view addSubview:scroll];
[scroll release];
for (int i = 0; i < kImageCount; i ++) {
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(kScreenWidth * i, 0, kScreenWidth, kScreenHeigth)];
imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"%@%02d", kImageName, i + 1]];
if (i == kImageCount - 1) {
//如果是最后一张,添加轻拍手势
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleIntoMain:)];
//打开imageView的用户交互
imageView.userInteractionEnabled = YES;
[imageView addGestureRecognizer:tap];
[tap release];
}
[scroll addSubview:imageView];
[imageView release];
}
}
- (void)setUpPageControl {
UIPageControl *pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake((kScreenWidth - 200)/2, kScreenHeigth - 50, 200, 30)];
pageControl.numberOfPages = kImageCount;
pageControl.tag = kPageControlTag;
pageControl.pageIndicatorTintColor = [UIColor grayColor];
[self.view addSubview:pageControl];
[pageControl release];
}
//实现跳转
-(void)handleIntoMain:(UITapGestureRecognizer *)tap {
//当点击最后一张时,将内容存储到NSUserDefaults中,单例模式
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setBool:YES forKey:KFirstMark];//给一个key值,返回BOOL值,如果为NO的话每次进入都会进行引导
//立即同步 (立即执行存储任务)
[defaults synchronize];
//2. 进入程序主页面
MainViewController *mainVC = [[MainViewController alloc] init];
//更改window的根视图控制器为主页面视图控制器
//单例模式,只初始化一次,在整个程序运行期间始终存在 是一个类方法
//[UIApplication sharedApplication] 得到一个对象
[UIApplication sharedApplication].keyWindow.rootViewController = mainVC;
[mainVC release];
}
#pragma mark -- UIScrolViewDelegate--
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
//获取scrollView 的偏移量
NSInteger index = scrollView.contentOffset.x/kScreenWidth;
//获取pageControl
UIPageControl *pageControl = (UIPageControl *)[self.view viewWithTag:kPageControlTag];
//设置pageControl的当前页
pageControl.currentPage = index;
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
@end