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

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