#import "ViewController.h"//屏幕宽度#define SCREEN_WIDTH [UIScreen mainScreen].bounds.size.width//图片高度#define IMG_HEIGHT 180//要显示的图片总数#define MAX_SIZE 7#import "ViewController.h"@interface ViewController () <UIScrollViewDelegate>//滚动视图@property (strong, nonatomic) UIScrollView *loopScrollView;//指示器@property (strong, nonatomic) UIPageControl *pageIndicator;//要展示的图片数组@property(strong, nonatomic) NSMutableArray *imgArray;@end@implementation ViewController//懒加载数组-(NSMutableArray *)imgArray{if(_imgArray == nil){_imgArray = [[NSMutableArray alloc]initWithCapacity:MAX_SIZE];//在要展示的5张图片的前后各加一张图片,第一张前面加第五张,第五张后面加第一张[_imgArray addObject:[UIImage imageNamed:@"img_05.jpg"]];for (int i = 1; i< MAX_SIZE - 1; i++) {NSString *imgName = [[NSString alloc]initWithFormat:@"img_0%d.jpg", i];[_imgArray addObject:[UIImage imageNamed:imgName]];}[_imgArray addObject:[UIImage imageNamed:@"img_01.jpg"]];}return _imgArray;}- (void)viewDidLoad {[super viewDidLoad];[self setupScrollView];[self setupPageControl];}/** * 创建UIScrollView并设置其属性 */-(void)setupScrollView{UIScrollView *sc = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 64, SCREEN_WIDTH, IMG_HEIGHT)];//创建UIImageView并添加到UIScrollView中for (int i = 0; i< MAX_SIZE; i++) {UIImageView *img = [[UIImageView alloc]initWithImage:[self.imgArray objectAtIndex:i]];img.frame = CGRectMake(SCREEN_WIDTH * i, 0, SCREEN_WIDTH, IMG_HEIGHT);[sc addSubview:img];}//设置UIScrollView的属性sc.contentSize = CGSizeMake(SCREEN_WIDTH * self.imgArray.count, IMG_HEIGHT);sc.showsHorizontalScrollIndicator = NO;sc.pagingEnabled = YES;//刚开始应该滚动到第二张显示,因为第一张其实是最后一张图片[sc setContentOffset:CGPointMake(SCREEN_WIDTH, 0) animated:NO];//设置代理并添加到当前view中sc.delegate = self;[self.view addSubview:sc];self.loopScrollView = sc;}/** * 创建UIPageControl并设置其属性 */-(void)setupPageControl{//注意frame,这样设置可以居中显示UIPageControl *pc = [[UIPageControl alloc]initWithFrame:CGRectMake(self.view.center.x - 50, CGRectGetMaxY(self.loopScrollView.frame) - 25 , 100, 25)];//设置UIPageControl的属性并添加到当前view中pc.numberOfPages = MAX_SIZE - 2;pc.currentPage = 0;pc.pageIndicatorTintColor = [UIColor redColor];[self.view addSubview:pc];self.pageIndicator = pc;}//UIScrollView的代理方法,在该方法中改变UIPageControl并且处理边缘滚动-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{//获取当前UIScrollView的位置CGPoint offset = [scrollView contentOffset];//算出滚动到第几页int currentPage = offset.x/SCREEN_WIDTH;//设置UIPageControlself.pageIndicator.currentPage = currentPage - 1;//对最后一张和第一张要进行特殊处理//1、如果是第一张if (currentPage == 0) {//下面两个方法任选其一都可以达到效果,但是注意动画一定要设置为NO,不然会有视觉会有辣眼睛的感觉//方法1[self.loopScrollView setContentOffset:CGPointMake(SCREEN_WIDTH * (MAX_SIZE-2), 0) animated:NO];//方法2,该方法要求设置contentSize时,任一方向就算不滚动也不能为0,否则无效//[self.loopScrollView scrollRectToVisible:CGRectMake(SCREEN_WIDTH * (MAX_SIZE-2), 0, SCREEN_WIDTH, IMG_HEIGHT) animated:NO];self.pageIndicator.currentPage = MAX_SIZE - 2;}//2、如果是最后一张else if(currentPage == MAX_SIZE - 1) {[self.loopScrollView setContentOffset:CGPointMake(SCREEN_WIDTH, 0) animated:NO];//[self.loopScrollView scrollRectToVisible:CGRectMake(SCREEN_WIDTH, 0, SCREEN_WIDTH, IMG_HEIGHT) animated:NO];self.pageIndicator.currentPage = 0;}}@end实现效果
总结
好了,以上就是这篇文章的全部内容了,其实实现轮播现在最好的方案应该是使用UICollectionView,因为它是利用重用机制来实现的,性能会好很多,代码写起来类似。希望本文的内容对大家的学习或者工作能带来一定的帮助,如果有疑问大家可以留言交流。