ScrollView 实现循环轮播
Posted 山水间
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ScrollView 实现循环轮播相关的知识,希望对你有一定的参考价值。
1 #import "MassageViewController.h" 2 @interface MassageViewController()<UIScrollViewDelegate> 3 @property (nonatomic,strong) UIScrollView *PicScrollView; 4 @property (nonatomic,strong) UIPageControl *PicCtr; 5 @property (nonatomic,strong) NSTimer *timer; 6 @property (nonatomic,strong) NSMutableArray *picArr; 7 @property (nonatomic,strong) UIImageView *PicImageView; 8 @property (nonatomic,strong) UIImageView *lastImageView; 9 @end 10 11 @implementation MassageViewController 12 -(NSMutableArray *)picArr{ 13 if (self.picArr==nil) { 14 _picArr = [NSMutableArray array]; 15 } 16 return _picArr; 17 } 18 19 -(void)viewDidLoad{ 20 self.picArr = [self loadImageData]; 21 self.PicScrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 150, self.view.frame.size.width, 200)]; 22 _PicScrollView.pagingEnabled = YES; 23 _PicScrollView.delegate = self; 24 _PicScrollView.showsHorizontalScrollIndicator = NO; 25 _PicScrollView.tag = 1000; 26 _PicScrollView.contentSize = CGSizeMake(self.PicScrollView.width * (_picArr.count), 0); 27 for (int i =0; i<_picArr.count; i++) { 28 CGFloat imgX = self.view.frame.size.width * i; 29 self.PicImageView = [[UIImageView alloc]initWithFrame:CGRectMake(imgX, 0, self.view.frame.size.width, 200)]; 30 _PicImageView.image = [_picArr objectAtIndex:i]; 31 [_PicScrollView addSubview:_PicImageView]; 32 } 33 CGFloat imgX = CGRectGetWidth(self.view.frame)*_picArr.count; 34 _lastImageView = [[UIImageView alloc]initWithFrame:CGRectMake(imgX, 0, self.view.frame.size.width, 200)]; 35 _lastImageView.image = [_picArr objectAtIndex:0]; 36 [_PicScrollView addSubview:_lastImageView]; 37 38 39 [self.view addSubview:_PicScrollView]; 40 self.PicCtr = [[UIPageControl alloc]initWithFrame:CGRectMake((self.PicScrollView.width-150)/2, self.PicScrollView.y+200-40, 150, 30)]; 41 _PicCtr.pageIndicatorTintColor = [UIColor grayColor]; 42 _PicCtr.currentPageIndicatorTintColor = [UIColor redColor]; 43 _PicCtr.numberOfPages = 5; 44 _PicCtr.currentPage = 0; 45 [self.view addSubview:_PicCtr]; 46 [self.view bringSubviewToFront:_PicCtr]; 47 48 self.timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(scrollImage) userInfo:nil repeats:YES]; 49 //修改优先级与其他控件一样 50 //获取当前消息循环对象 51 NSRunLoop *runLoop = [NSRunLoop currentRunLoop]; 52 //设置优先级 53 [runLoop addTimer:self.timer forMode:NSRunLoopCommonModes]; 54 } 55 56 -(NSMutableArray *)loadImageData{//加载图片 57 NSMutableArray *arr = [NSMutableArray array]; 58 for (int i = 0; i<5; i++) {
//为了节约内存,建议使用路径加载,注意图片不要放在Assets.xcassets里面,否则读取不到 59 NSString *imgName = [NSString stringWithFormat:@"%02d.jpg",i+1];//注意.jpg格式,图片格式统一 60 NSString *imgPath = [[NSBundle mainBundle]pathForResource:imgName ofType:nil]; 61 UIImage *image = [UIImage imageWithContentsOfFile:imgPath]; 62 [arr addObject:image]; 63 } 64 return arr; 65 } 66 67 -(UIStatusBarStyle)preferredStatusBarStyle{ 68 return UIStatusBarStyleLightContent; 69 } 70 71 -(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{ 72 //停止计时器 73 [self.timer invalidate]; 74 self.timer = nil; 75 } 76 77 -(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{ 78 79 //r如果放到didscroll里面,每次自动播放都会调用,我们想要手指拖动时才调用,所以放到这里面,每次拖到第几张,白点就点亮 80 CGFloat offsetX = self.PicScrollView.contentOffset.x + self.PicScrollView.frame.size.width/2; 81 int index = offsetX/self.PicScrollView.width; 82 _PicCtr.currentPage = index; 83 84 //重新启用计时器 85 self.timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(scrollImage) userInfo:nil repeats:YES]; 86 //修改优先级与其他控件一样 87 //获取当前消息循环对象 88 NSRunLoop *runLoop = [NSRunLoop currentRunLoop]; 89 //设置优先级 90 [runLoop addTimer:self.timer forMode:NSRunLoopCommonModes]; 91 } 92 93 -(void)scrollImage{ 94 //在scrollview后面加一个imageview,和第一个图一样。 95 float offsetx = _PicScrollView.contentOffset.x; 96 offsetx += CGRectGetWidth(self.view.frame); 97 if (offsetx == CGRectGetWidth(self.view.frame)*_picArr.count) { 98 _PicCtr.currentPage = 0; 99 }else{ 100 _PicCtr.currentPage = offsetx/CGRectGetWidth(self.view.frame); 101 } 102 if (offsetx>CGRectGetWidth(self.view.frame)*_picArr.count) { 103 //直接复制速度很快直接和lastImageView一样,看起来就是循环播放的 104 _PicScrollView.contentOffset= CGPointMake(0, 0); 105 _PicCtr.currentPage = 1; 106 [_PicScrollView setContentOffset:CGPointMake(CGRectGetWidth(self.view.frame), 0) animated:YES]; 107 }else{ 108 [_PicScrollView setContentOffset:CGPointMake(offsetx, 0) animated:YES]; 109 } 110 } 111 @end
以上是关于ScrollView 实现循环轮播的主要内容,如果未能解决你的问题,请参考以下文章