首先,准备工作就不说了,新建一个工程,创建一个 UIViewController 子类,并在应用中加入三个图片(1,2,3.jpg)
设置创建的 UIViewController 子类 为应用根视图控制对象
AXRollController *rollController = [[AXRollController alloc] init]; [[self window] setRootViewController:rollController];
在 UIViewController 子类中添加 scrollView,imageViewOne,imageViewTwo,imageViewThree 实例变量。并重写 viewDidLoad 方法,初始化实例变量,将 ImageView 的视图都加入到 scrollView 的子视图
/* 获取应用屏幕Frame */ CGRect screenRect = [[UIScreen mainScreen] applicationFrame]; /* 创建 UIScrollView, 并加入视图控制器视图 */ scrollView = [[UIScrollView alloc] initWithFrame:screenRect]; [scrollView setPagingEnabled:YES]; [[self view] addSubview:scrollView]; /* 获得 scroll 取景, 设置三倍宽度 */ CGRect bigRect = screenRect; bigRect.size.width *= 3.0; /* 设置取景宽度 */ [scrollView setContentSize:bigRect.size]; /* 创建第一个 UIImageView, 并加入到 scrollView */ imageViewOne = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"1.jpg"]]; [imageViewOne setContentMode:UIViewContentModeScaleAspectFit]; [scrollView addSubview:imageViewOne]; /* 创建第二个 UIImageView, 并加入到 scrollView */ imageViewTwo = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"2.jpg"]]; [imageViewTwo setContentMode:UIViewContentModeScaleAspectFit]; [scrollView addSubview:imageViewTwo]; /* 创建第三个 UIImageView, 并加入到 scrollView */ imageViewThree = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"3.jpg"]]; [imageViewThree setContentMode:UIViewContentModeScaleAspectFit]; [scrollView addSubview:imageViewThree]; /* 初始化三个 UIImageView 的 Frame */ [self setAllViewFrame]; /* 设置 scrollView 初始偏移量, 显示第二个 UIImageView */ CGPoint p = CGPointZero; p.x = scrollView.frame.size.width; [scrollView setContentOffset:p animated:NO]; /* 设置代理对象 */ [scrollView setDelegate:self];
setAllViewFrame,设置三个 UIImageView 的 Frame
[imageViewOne setFrame:CGRectMake(scrollView.frame.origin.x, scrollView.frame.origin.y, scrollView.frame.size.width, scrollView.frame.size.height)]; [imageViewTwo setFrame:CGRectMake(scrollView.frame.size.width, scrollView.frame.origin.y, scrollView.frame.size.width, scrollView.frame.size.height)]; [imageViewThree setFrame:CGRectMake(scrollView.frame.size.width * 2, scrollView.frame.origin.y, scrollView.frame.size.width, scrollView.frame.size.height)];
实现 UIScrollerView 的代理方法,scrollViewDidEndDecelerating:。当前 page(0,1[当前],2) 页为 1,则代表未动,返回;当前 page 页为 0,则代表右移,0->1/2->0/1->2;当前 page 页为 1,则代表左移,3->1/0->3/1->0。
NSLog(@"%@", NSStringFromSelector(_cmd)); CGFloat pageWidth = scrView.frame.size.width; NSLog(@"pageWidth = %f", pageWidth); NSLog(@"offset = %f", scrView.contentOffset.x); int page = floor((scrView.contentOffset.x - pageWidth / 2) / pageWidth) + 1; NSLog(@"page = %d", page); if (page == 1) { /* 当前页,未动,返回 */ return; } else if (page == 0) { /* 前一页,右移 */ [self imageMoveToRight]; } else { /* 前一页,左移 */ [self imageMoveToLeft]; } CGPoint p = CGPointZero; p.x = pageWidth; [scrView setContentOffset:p animated:NO];
右移的方法,imageMoveRight
UIImageView *tempView = imageViewTwo; imageViewTwo = imageViewOne; imageViewOne = imageViewThree; imageViewThree = tempView; /* 重设置所有 UIImageView 视图 Frame */ [self setAllViewFrame];
左移的方法,imageMoveLeft
UIImageView *tempView = imageViewTwo; imageViewTwo = imageViewThree; imageViewThree = imageViewOne; imageViewOne = tempView; /* 重设置所有 UIImageView 视图 Frame */ [self setAllViewFrame];
基于 Xcode 5.0.2 编写