您好,登錄后才能下訂單哦!
這篇文章主要介紹IOS開發(fā)之UIScrollView如何實現(xiàn)圖片輪播器的無限滾動,文中介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們一定要看完!
IOS開發(fā)之UIScrollView實現(xiàn)圖片輪播器的無限滾動
簡介
在現(xiàn)在的一些App中常常見到圖片輪播器,一般用于展示廣告、新聞等數(shù)據(jù),在iOS內并沒有現(xiàn)成的控件直接實現(xiàn)這種功能,但是通過UIScrollView的允許分頁設置,可以實現(xiàn)滾動輪播的功能。
輪播原理
UIScrollView對象有pagingEnable成員,如果設置為YES,那么每一個scrollView尺寸這么大的區(qū)域就會被當作一頁,在滾動時會根據(jù)滾動的比例自動計算應該切換到哪一頁。
無限滾動原理
要實現(xiàn)無限滾動,需要額外的兩張圖片,假設我們的圖片有五張,存在images數(shù)組中,那么我們在將圖片插入到scrollView中時,在第一張圖片前面插入一個最后一張圖片作為輔助圖片,在最后一張后面插入一個第一張圖片作為輔助圖片。這樣,當滾動到第一張前面一張時,在頁面切換結束后無動畫的切換scrollView的偏移量為最后一張圖片(不包含最后一張后面的第一張那個輔助圖片),這樣就實現(xiàn)了由輔助圖片到真實圖片的過渡,之所以設置輔助圖片是為了在滾動中看到那個真實圖片。同理,當滾動到最后一張的后面一張時,我們吧scrollView的偏移量設置為第一張圖片即可。
具體的代碼實現(xiàn)
這個代碼是在開發(fā)一個項目中所寫的,已經封裝稱一個View,只需要調用initWithFrame指定輪播器尺寸,然后通過設置images成員的值即可實現(xiàn)無限滾動的輪播。
// .h // // ESPicPageView.h // 享技 // // Created by 11 on 11/30/15. // Copyright © 2015 soulghost. All rights reserved. // #import <UIKit/UIKit.h> @interface ESPicPageView : UIView @property (nonatomic, strong) NSArray *images; @end // -------------------------------------------- // .m // // ESPicPageView.m // 享技 // // Created by 11 on 11/30/15. // Copyright © 2015 soulghost. All rights reserved. // #import "ESPicPageView.h" #import "UIImageView+WebCache.h" @interface ESPicPageView () <UIScrollViewDelegate> @property (nonatomic, weak) UIScrollView *scrollView; @property (nonatomic, weak) UIPageControl *pageControl; @property (nonatomic, assign) CGFloat imgW; @property (nonatomic, assign) CGFloat imgH; @property (nonatomic, strong) NSTimer *timer; @property (nonatomic, strong) NSArray *imageViews; @property (nonatomic, assign) NSInteger imageCount; @end @implementation ESPicPageView - (instancetype)initWithFrame:(CGRect)frame{ if (self = [super initWithFrame:frame]) { self.backgroundColor = [UIColor blueColor]; UIScrollView *scrollView = [[UIScrollView alloc] init]; self.scrollView = scrollView; self.scrollView.delegate = self; self.scrollView.pagingEnabled = YES; self.scrollView.showsHorizontalScrollIndicator = NO; self.scrollView.backgroundColor = [UIColor whiteColor]; [self addSubview:scrollView]; self.imgW = frame.size.width; self.imgH = frame.size.height; [self setNeedsLayout]; UIPageControl *pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(frame.size.width - 50, frame.size.height - 10, 0, 0)]; self.pageControl = pageControl; self.pageControl.numberOfPages = 1; [self addSubview:pageControl]; self.timer = [NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(nextImage) userInfo:nil repeats:YES]; [[NSRunLoop currentRunLoop] addTimer:self.timer forMode:NSRunLoopCommonModes]; } return self; } - (void)nextImage{ NSInteger page = self.pageControl.currentPage; page = self.pageControl.currentPage + 1; CGPoint offset = CGPointMake((1 + page) * self.imgW, 0); [self.scrollView setContentOffset:offset animated:YES]; } - (void)setupImageViews{ for (int i = 0; i < self.images.count + 2; i++) { UIImageView *imageView = [[UIImageView alloc] init]; CGFloat imageX = i * self.imgW; CGFloat imageY = 0; CGFloat imageW = self.imgW; CGFloat imageH = self.imgH; imageView.frame = CGRectMake(imageX, imageY, imageW,imageH); [self.scrollView insertSubview:imageView atIndex:0]; } } - (NSArray *)imageViews{ if (_imageViews == nil) { NSMutableArray *arr = [NSMutableArray array]; for (int i = 0; i < self.images.count + 2; i++) { UIImageView *imageView = [[UIImageView alloc] init]; CGFloat imageX = i * self.imgW; CGFloat imageY = 0; CGFloat imageW = self.imgW; CGFloat imageH = self.imgH; imageView.frame = CGRectMake(imageX, imageY, imageW,imageH); [self.scrollView insertSubview:imageView atIndex:0]; [arr addObject:imageView]; } _imageViews = arr; } return _imageViews; } - (void)setImages:(NSArray *)images{ _images = images; self.imageCount = images.count; self.pageControl.numberOfPages = self.imageCount; [self addPics]; } - (void)addPics{ for (int i = 0; i < self.images.count + 2; i++) { UIImageView *imageView = self.imageViews[i]; if (i == 0) { imageView.image = [self.images lastObject]; }else if(i == self.images.count + 1){ imageView.image = [self.images firstObject]; }else{ imageView.image = self.images[i - 1]; } } } - (void)layoutSubviews{ [super layoutSubviews]; self.scrollView.frame = self.bounds; self.scrollView.contentSize = CGSizeMake((self.imageCount + 2) * self.imgW, 0); [self.scrollView setContentOffset:CGPointMake(self.imgW, 0) animated:NO]; } - (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{ [self.timer invalidate]; self.timer = nil; } - (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{ self.timer = [NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(nextImage) userInfo:nil repeats:YES]; [[NSRunLoop currentRunLoop ] addTimer:self.timer forMode:NSRunLoopCommonModes]; } - (void)scrollViewDidScroll:(UIScrollView *)scrollView{ if (scrollView.contentOffset.x == self.imgW * (self.imageCount + 1)) { [self.scrollView setContentOffset:CGPointMake(self.imgW, 0) animated:NO]; }else if(scrollView.contentOffset.x == 0){ [self.scrollView setContentOffset:CGPointMake(self.imgW * (self.imageCount), 0) animated:NO]; } self.pageControl.currentPage = (self.scrollView.contentOffset.x + self.imgW * 0.5f) / self.imgW - 1; } @end
以上是“IOS開發(fā)之UIScrollView如何實現(xiàn)圖片輪播器的無限滾動”這篇文章的所有內容,感謝各位的閱讀!希望分享的內容對大家有幫助,更多相關知識,歡迎關注億速云行業(yè)資訊頻道!
免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經查實,將立刻刪除涉嫌侵權內容。