UIScrollView和UIPageControl配合使用完成引用介绍页面

// 直接代码
// 头文件
//
//  RootViewController.h
//  UIScrollView
//
// 需要遵守UIScrollViewDelegate协议

#import

@interface RootViewController : UIViewController <UIScrollViewDelegate>

@property (nonatomic, retain) UIScrollView *userGuideScrollView;
@property (nonatomic, retain) UIPageControl *scrollPageControl;

@end
// 目标文件
//
//  RootViewController.m
//  UIScrollView
// 
// 实现思路
//  1. 在主视图中添加一个高度和宽度都和主视图相同的UIScrollView,然后设置它内容的大小为有多少屏幕图片的大小(高度和主视图相同,宽度就是有几张图片,就用主视图宽度乘以几)
//  2. 把准备好的图片添加到UIscrollView中,然后把UIscrollView添加到当前的View上
//  3. 然后添加UIPageControl在视图上面
//  4. 需要实现一个UIPageControl的监听方法,点击时会改变视图
//  5. 实现UIScrollViewDelegate协议中的方法,实现滑动UIScrollView的时候,让UIPageControl也进行相应的移动
//  6. 最后重写dealloc方法,释放属性内存
//

#import "RootViewController.h"

@interface RootViewController ()

@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];

// 添加UIScrollView
self.userGuideScrollView = [[[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)] autorelease]; // 初始化位置和大小
self.userGuideScrollView.contentSize = CGSizeMake(self.view.frame.size.width * 11, self.view.frame.size.height); // 设置存放的内容的大小
_userGuideScrollView.pagingEnabled = YES; // 设置scrollView按一整页滑动
_userGuideScrollView.delegate = self; // 设置代理为当前
_userGuideScrollView.userInteractionEnabled = YES; // 设置可以与用户交互
_userGuideScrollView.showsHorizontalScrollIndicator = YES; // 显示滑动时候横向的滚动条
// 添加图片到UIScrollView中
for (int i = 1; i <= 11; i++) {
    UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:[NSString stringWithFormat:@"image%d", i]]];
    imageView.frame = CGRectMake((i - 1) * self.view.frame.size.width, 0, self.view.frame.size.width, self.view.frame.size.height);
    [_userGuideScrollView addSubview:imageView];
    [imageView release], imageView = nil;
}
// 添加到当前的View
[self.view addSubview:_userGuideScrollView];


// 初始化UIPageController
self.scrollPageControl = [[[UIPageControl alloc] init] autorelease];
_scrollPageControl.frame = CGRectMake(0, self.view.frame.size.height - 60, self.view.frame.size.width, 30); // 设置位置
_scrollPageControl.numberOfPages = 11; // 设置有多少个小点点
_scrollPageControl.currentPage = 0; // 设置当前是哪个点点
_scrollPageControl.pageIndicatorTintColor = [UIColor blueColor]; // 没有选中的点点的颜色
_scrollPageControl.currentPageIndicatorTintColor = [UIColor yellowColor]; // 当前点点的颜色
[_scrollPageControl addTarget:self action:@selector(scrollPageControlAction:) forControlEvents:UIControlEventValueChanged]; // 设置监听事件
_scrollPageControl.highlighted = YES;
[self.view addSubview:_scrollPageControl];

}

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


#pragma mark - 实现---scrollPageControlAction:监听事件
- (void)scrollPageControlAction:(UIPageControl *) sender {
//令UIScrollView做出相应的滑动显示
CGSize viewSize = _userGuideScrollView.frame.size;
CGRect rect = CGRectMake(sender.currentPage * viewSize.width, 0, viewSize.width, viewSize.height);
[_userGuideScrollView scrollRectToVisible:rect animated:YES];
}


#pragma mark - 重写----scrollViewDelegate中的代理方法 实现滑动
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
// 计算偏移量
int index = abs(scrollView.contentOffset.x / scrollView.frame.size.width);
// 根据偏移量来设置pageControl
_scrollPageControl.currentPage = index;
}


#pragma mark - 重写----dealloc方法
- (void)dealloc {
// 释放属性的内存
[_scrollPageControl release], _scrollPageControl = nil;
[_userGuideScrollView release], _userGuideScrollView = nil;
// 调用父类的dealloc方法
[super dealloc];
}


@end