iOS用户引导页的简单实现

//
//  RootViewController.m
//  LessonUIPageControl
//
//  Created by lanouhn on 14-8-29.
//  Copyright (c) 2014年 [email protected] 陈聪雷. All rights reserved.
//

#import "RootViewController.h"

@interface RootViewController ()<UIScrollViewDelegate>

@end

@implementation RootViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor grayColor];
    
    //存储用户的偏好设置, 存储在本地, 比如:程序是否是第一次加载
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    if (![userDefaults boolForKey:@"aa"]) {
        [self setupFirstLanchView];
        [userDefaults setBool:YES forKey:@"aa"];
        //立即同步
        [userDefaults synchronize];
    }
}

//创建程序第一次加载要显示的视图
- (void)setupFirstLanchView
{
    [self setupScrollView];
    [self setupPageControl];
    
}

- (void)setupScrollView
{
    UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    scrollView.delegate = self;
    scrollView.tag = 200;
    scrollView.contentSize = CGSizeMake(320 * 6, [UIScreen mainScreen].bounds.size.height);
    scrollView.showsHorizontalScrollIndicator = NO;
    scrollView.pagingEnabled = YES;
    [self.view addSubview:scrollView];
    [scrollView release];
    
    for (int i = 0; i < 6; i++) {
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(320 * i, 0, 320, [UIScreen mainScreen].bounds.size.height)];
        imageView.image = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"v6_guide_%d", i + 1]ofType:@"png"]];
        [scrollView addSubview:imageView];
        [imageView release];
        
    }
}

- (void)setupPageControl
{
    //UIPageControl 1 表示页数 2 表示当前正出处于第几页 3 点击切换页数
    UIPageControl *pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(10, [UIScreen mainScreen].bounds.size.height - 40, 300, 20)];
    pageControl.tag = 100;
    //设置表示的页数
    pageControl.numberOfPages = 6;
    //设置选中的点
    pageControl.currentPage = 0;
    //设置为选中点的颜色
    pageControl.pageIndicatorTintColor = [UIColor grayColor];
    //设置选中点的颜色
    pageControl.currentPageIndicatorTintColor = [UIColor orangeColor];
    //添加响应事件
    [pageControl addTarget:self action:@selector(handlePageControl:) forControlEvents:UIControlEventValueChanged];
    [self.view addSubview:pageControl];
    [pageControl release];

}

- (void)handlePageControl:(UIPageControl *)pageControl
{
    NSLog(@"%d", pageControl.currentPage);
    //切换pageControl, 对应切换scrollView对应不同的界面
    UIScrollView *scrollView = (UIScrollView *)[self.view viewWithTag:200];
    //
    [scrollView setContentOffset:CGPointMake(320 * pageControl.currentPage, 0) animated:YES];
    
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    UIPageControl *pageControl = (UIPageControl *)[self.view viewWithTag:100];
    pageControl.currentPage = scrollView.contentOffset.x / 320;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章