RootViewController.m
@interface RootViewController ()<UIScrollViewDelegate>
@property (nonatomic, retain) UIPageControl *pageControl;
@property (nonatomic, retain) UIScrollView *scrollView;
@property (nonatomic, retain) NSMutableArray *imageViewArr;
@end
@implementation RootViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
/*
启动定时器,实现scrollerView的自动转换
*/
// [NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(runTimePage) userInfo:nil repeats:YES];
/*
初始化ScrollerView
*/
self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 460)];
[self.scrollView setBounces:YES];
[self.scrollView setPagingEnabled:YES];//设置翻页
[self.scrollView setUserInteractionEnabled:YES];
/*
水平和垂直的滚动条会作为两个子视图添加到scrollerView中,
如果把滚动条效果关闭,则不会添加到scrollerView中
*/
[self.scrollView setShowsHorizontalScrollIndicator:NO];
[self.scrollView setShowsVerticalScrollIndicator:NO];
[self.scrollView setDelegate:self];
[self.view addSubview:self.scrollView];
[self.scrollView release];
/*
初始化数组
*/
self.imageViewArr = [NSMutableArray array];
for (NSInteger i = 1; i < 5; i++) {
[self.imageViewArr addObject:[NSString stringWithFormat:@"image%ld.jpg",i]];
}
/*
初始化Pagecontrol
*/
self.pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(120, 480, 100, 28)];
[self.pageControl setCurrentPageIndicatorTintColor:[UIColor redColor]];
[self.pageControl setPageIndicatorTintColor:[UIColor blackColor]];
[self.pageControl setNumberOfPages:[self.imageViewArr count]];
[self.pageControl setCurrentPage:0];
[self.pageControl addTarget:self action:@selector(turnPage) forControlEvents:UIControlEventValueChanged];
[self.view addSubview:self.pageControl];
[self.pageControl release];
/*
创建四个图片,添加到scrollerView
*/
for (NSInteger i = 0; i < 4; i++) {
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:self.imageViewArr[i]]];
[imageView setFrame:CGRectMake(320 * (i + 1), 0, 320, 460)];
[self.scrollView addSubview:imageView];
[imageView release];
}
/*
取数组最后一张图片,放在第 0 页
*/
UIImageView *imageView1 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:self.imageViewArr[(self.imageViewArr.count - 1)]]];
[imageView1 setFrame:CGRectMake(0, 0, 320, 460)];
[self.scrollView addSubview:imageView1];
[imageView1 release];
/*
取数组的第一张图片放在最后一页
*/
UIImageView *imageView2 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:self.imageViewArr[0]]];
[imageView2 setFrame:CGRectMake(320 * (self.imageViewArr.count + 1), 0, 320, 460)];
[self.scrollView addSubview:imageView2];
[imageView2 release];
[self.scrollView setContentSize:CGSizeMake(320 * ([self.imageViewArr count] + 2), 460)];
/*
设置初次偏移量
*/
[self.scrollView setContentOffset:CGPointMake(0, 0)];
/*
此方法滚动查看内容,以便由rect定义的区域仅仅是滚动视图内是可见的。
如果该地区已是可见的,该方法什么都不做
*/
[self.scrollView scrollRectToVisible:CGRectMake(320, 0, 320, 460) animated:NO];
NSLog(@"count = %ld",self.imageViewArr.count);
}
#pragma mark - scrollerView的委托函数
#pragma mark - 当scrollerView滚动时触发的方法
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
int page = (int)self.scrollView.contentOffset.x / 320;
page -- ;// 默认从第二页开始
[self.pageControl setCurrentPage:page];
// NSLog(@"%f",self.scrollView.contentOffset.x);
}
#pragma mark - 当scrollerView滚动减速后停止时触发的方法
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
int currentPage = (int)self.scrollView.contentOffset.x / 320;
NSLog(@"%f",self.scrollView.contentOffset.x);
if (currentPage == 0) {
[self.scrollView scrollRectToVisible:CGRectMake(320 * self.imageViewArr.count, 0, 320, 460) animated:NO];
NSLog(@"0");
}else if(currentPage == ([self.imageViewArr count] + 1)){
[self.scrollView scrollRectToVisible:CGRectMake(320, 0, 320, 460) animated:NO];
NSLog(@"5");
}
}
#pragma mark - pageControl选择器方法
-(void)turnPage{
NSInteger page = self.pageControl.currentPage;
[self.scrollView scrollRectToVisible:CGRectMake((320 * page + 1), 0, 320, 460) animated:NO];
}
#pragma mark - 定时器方法
-(void)runTimePage{
NSInteger page = self.pageControl.currentPage; // 获取当前的page
page++;
page = page > 3 ? 0 : page ;
self.pageControl.currentPage = page;
[self turnPage];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(void)dealloc{
[self.pageControl release];
[self.scrollView release];
[self.imageViewArr release];
[super dealloc];
}