iOS--轮播图实现
Posted 囧囧(>_<)
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了iOS--轮播图实现相关的知识,希望对你有一定的参考价值。
- (void)viewDidLoad { [super viewDidLoad]; /** * 轮播图 * * @return nil */ //图片数组 imageArr = @[@"scroll.png",@"record.png",@"state2.png"]; //scrollview的宽和高 scrollWidth = ScreenWidth - 20; scrollHeight = (homeViewHeight-5)*0.3; //设定scrollview的contentSize大小,如果contentSize的高大于scrollview的高就会出现上下拖动的现象,一般设成相等或为0 scrollView.contentSize = CGSizeMake(scrollWidth*(self.imageArr.count+2), scrollHeight); //默认从第一页开始 [scrollView setContentOffset:CGPointMake(0, 0)]; [scrollView scrollRectToVisible:CGRectMake(scrollWidth, 0, scrollWidth, scrollHeight) animated:NO]; scrollView.delegate = self; //pageControl的点的个数 pageControl.numberOfPages = self.imageArr.count; //从第一页开始 pageControl.currentPage = 0; //轮播 [pageControl addTarget:self action:@selector(turnPage) forControlEvents:UIControlEventValueChanged]; //添加图片 for (int i = 0; i < imageArr.count; i ++) { UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(scrollWidth*i+scrollWidth, 0, scrollWidth, scrollHeight)]; imageView.image = [UIImage imageNamed:imageArr[i]]; imageView.userInteractionEnabled = YES; [scrollView addSubview:imageView]; } //取最后一张图片放在第0页 UIImageView *firstImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, scrollWidth, scrollHeight)]; firstImageView.image = [UIImage imageNamed:[imageArr lastObject]]; [scrollView addSubview:firstImageView]; //取第一张图片放在最后一页 UIImageView *lastImageView = [[UIImageView alloc] initWithFrame:CGRectMake(scrollWidth*(imageArr.count+1), 0, scrollWidth, scrollHeight)]; lastImageView.image = [UIImage imageNamed:[self.imageArr firstObject]]; [scrollView addSubview:lastImageView]; //定时器 循环播放 [self addTimer]; } #pragma mark--------轮播图实现,UIPageControl控制自动轮播--- - (void)turnPage { NSInteger page = pageControl.currentPage; // 获取当前的page [scrollView scrollRectToVisible:CGRectMake(scrollWidth*(page+1),0,scrollWidth,scrollHeight) animated:NO]; // 触摸pagecontroller那个点点 往后翻一页 +1 } - (void)runTimePage { NSInteger page = pageControl.currentPage; // 获取当前的page page++; page = page > (imageArr.count - 1) ? 0 : page ; pageControl.currentPage = page; [self turnPage]; } - (void)addTimer { self.myTimer = [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(runTimePage) userInfo:nil repeats:YES]; } #pragma mark---UIScrollView代理方法,保证可以手动滑动 - (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView { //关闭定时器,避免与手动滑动冲突 [self.myTimer invalidate]; } - (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate { //当手动滑动结束后,开启定时器 [self addTimer]; } - (void)scrollViewDidScroll:(UIScrollView *)scrollView { CGFloat pagewidth = scrollWidth; NSInteger page = floor((scrollView.contentOffset.x - pagewidth/([imageArr count]+2))/pagewidth)+1; page --; // 默认从第二页开始 pageControl.currentPage = page; } - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView { CGFloat pagewidth = scrollWidth; NSInteger currentPage = floor((scrollView.contentOffset.x - pagewidth/ ([imageArr count]+2)) / pagewidth) + 1; if (currentPage==0) { [scrollView scrollRectToVisible:CGRectMake(scrollWidth * [imageArr count],0,scrollWidth,scrollHeight) animated:NO]; // 序号0 最后1页 } else if (currentPage==([imageArr count]+1)) { [scrollView scrollRectToVisible:CGRectMake(scrollWidth,0,scrollWidth,scrollHeight) animated:NO]; // 最后+1,循环第1页 } }
以上是关于iOS--轮播图实现的主要内容,如果未能解决你的问题,请参考以下文章