先看看後果圖:
詳細完成代碼:
1. 掌握器
// // AppDelegate.m // 無窮輪播器 // // Created by zhangmi on 16/5/16. // Copyright © 2016年 Paramount Pictures. All rights reserved. // #import "ViewController.h" #import "SNInfiniteScrollView.h" @interface ViewController () @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. NSMutableArray * images = [NSMutableArray array]; for (int i = 0; i < 5; i++) { NSString * imageName = [NSString stringWithFormat:@"ad_%02d", i]; //img01 UIImage * image = [UIImage imageNamed:imageName]; [images addObject:image]; } UIView * scrollView = [SNInfiniteScrollView scrollViewWithFrame:CGRectMake(0, 20, 414, 200) superView:self.view images:images scrollDirection:ScrollDirectionHorizontal pageIndicatorTintColor:[UIColor lightGrayColor] currentPageIndicatorTintColor:[UIColor orangeColor] imageViewcontentMode:UIViewContentModeScaleaspectFit]; [self.view addSubview:scrollView]; } @end
2. 顯示內容界面設置
// // AppDelegate.m // 無窮輪播器 // // Created by zhangmi on 16/5/16. // Copyright © 2016年 Paramount Pictures. All rights reserved. // #import "SNInfiniteScrollView.h" static int const ImageViewCount = 3; #define scrollViewWidth self.scrollView.frame.size.width #define scrollViewHeight self.scrollView.frame.size.height @interface SNInfiniteScrollView () <UIScrollViewDelegate> @property(weak, nonatomic) UIScrollView * scrollView; @property(weak, nonatomic) NSTimer * timer; /** pageIndex */ @property(nonatomic, assign) NSInteger pageIndex; @end @implementation SNInfiniteScrollView - (void)setImages:(NSArray<UIImage *> *)images { _images = images; // 設置頁碼 self.pageIndex = 0; // 設置內容 [self updateContent]; // 開端准時器 [self startTimer]; } /** 代碼創立的時刻挪用. */ - (instancetype)initWithFrame:(CGRect)frame { if (self = [super initWithFrame:frame]) { // 轉動視圖 UIScrollView * scrollView = [[UIScrollView alloc] init]; self.scrollView = scrollView; scrollView.delegate = self; // scroller屬性 scrollView.showsHorizontalScrollIndicator = NO; scrollView.showsVerticalScrollIndicator = NO; scrollView.pagingEnabled = YES; scrollView.bounces = NO; // 添加scrollView [self addSubview:scrollView]; // 圖片控件 for (int i = 0; i < ImageViewCount; i++) { UIImageView * imageView = [[UIImageView alloc] init]; // 圖片不變形處置. imageView.contentMode = self.imageViewcontentMode; [scrollView addSubview:imageView]; } } return self; } /** 結構 子控件, 只履行一次 */ - (void)layoutSubviews { [super layoutSubviews]; self.scrollView.frame = self.bounds; if (self.scrollDirection == ScrollDirectionVertical) { self.scrollView.contentSize = CGSizeMake(0, ImageViewCount * self.bounds.size.height); } else { self.scrollView.contentSize = CGSizeMake(ImageViewCount * self.bounds.size.width, 0); } for (int i = 0; i < ImageViewCount; i++) { UIImageView * imageView = self.scrollView.subviews[i]; if (self.scrollDirection == ScrollDirectionVertical) { imageView.frame = CGRectMake(0, i * self.scrollView.frame.size.height, self.scrollView.frame.size.width, self.scrollView.frame.size.height); } else { imageView.frame = CGRectMake(i * self.scrollView.frame.size.width, 0, self.scrollView.frame.size.width, self.scrollView.frame.size.height); } } // 設置內容 [self updateContent]; } #pragma mark - 內容更新 - (void)updateContent { // 設置圖片 for (int i = 0; i < self.scrollView.subviews.count; i++) { NSInteger pageIndex = self.pageIndex; // 遍歷每個imageView UIImageView * imageView = self.scrollView.subviews[i]; if (i == 0) { pageIndex--; } else if (i == 2) { pageIndex++; } if (pageIndex < 0) { pageIndex = self.images.count - 1; } else if (pageIndex >= self.images.count) { pageIndex = 0; } // 圖片角標 賦值給 imageView的tag imageView.tag = pageIndex; imageView.image = self.images[imageView.tag]; } // 設置偏移量在中央 // 不克不及應用帶動畫 contentOffset if (self.scrollDirection == ScrollDirectionVertical) { self.scrollView.contentOffset = CGPointMake(0, scrollViewHeight); } else { self.scrollView.contentOffset = CGPointMake(scrollViewWidth, 0); } } #pragma mark - <UIScrollViewDelegate> - (void)scrollViewDidScroll:(UIScrollView *)scrollView { // 找出最中央的誰人圖片控件 NSInteger page = self.pageIndex; CGPoint point = CGPointZero; for (int i = 0; i < self.scrollView.subviews.count; i++) { UIImageView * imageView = self.scrollView.subviews[i]; point = [scrollView convertPoint:imageView.frame.origin toView:self.superview]; //=****** other way ****************** stone *** if (self.scrollDirection == ScrollDirectionVertical) { if (ABS(point.y - self.frame.origin.y) < 1.0) { page = imageView.tag; } } else { if (ABS(point.x - self.frame.origin.x) < 1.0) { page = imageView.tag; } } } self.pageIndex = page; self.pageControl.currentPage = page; //拖動停止會挪用 [self updateContent]; //#warning mark - 沒有動畫正常 , 有動畫不動, 一向是原點 // [self updateContent]; // 沒有動畫正常 , 有動畫不動, 一向是原點 } /** 開端拖拽 */ - (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView { // 停滯准時器 [self stopTimer]; } /** 停止拖拽 */ - (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate { // 開啟准時器 [self startTimer]; } /** 加速終了 */ - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView { // 更新內容 , 假如contentOffset 不帶動畫的話 不走這個辦法 [self updateContent]; } /** 停止轉動動畫 */ // 這是保險的做法吧... 假如contentOffset 不帶動畫的話 不走這個辦法 - (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView { // 更新內容 [self updateContent]; } #pragma mark - 准時器處置 - (void)startTimer { NSTimer * timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(next:) userInfo:nil repeats:YES]; // [[NSRunLoop mainRunLoop] addTimer:timer forMode:NSRunLoopCommonModes]; [[NSRunLoop currentRunLoop] addTimer:timer forMode:NSRunLoopCommonModes]; self.timer = timer; } - (void)stopTimer { [self.timer invalidate]; self.timer = nil; } - (void)next:(NSTimer *)timer { if (self.scrollDirection == ScrollDirectionVertical) { [self.scrollView setContentOffset:CGPointMake(0, 2 * self.scrollView.frame.size.height) animated:YES]; } else { [self.scrollView setContentOffset:CGPointMake(2 * self.scrollView.frame.size.width, 0) animated:YES]; } } //=****** 簡略挪用 ****************** stone *** + (instancetype)scrollViewWithFrame:(CGRect)frame superView:(UIView *)superView images:(NSArray<UIImage *> *)images scrollDirection:(ScrollDirection)scrollDirection pageIndicatorTintColor:(UIColor *)pageIndicatorTintColor currentPageIndicatorTintColor:(UIColor *)currentPageIndicatorTintColor imageViewcontentMode:(UIViewContentMode)imageViewcontentMode { //=****** 添加自界說scrollView ****************** stone *** SNInfiniteScrollView * scrollView = [[SNInfiniteScrollView alloc] init]; scrollView.frame = frame; scrollView.imageViewcontentMode = imageViewcontentMode; scrollView.scrollDirection = scrollDirection; //=****** 添加image ****************** stone *** scrollView.images = images; //=****** 添加pageControl ****************** stone *** UIPageControl * pageControl = [[UIPageControl alloc] init]; scrollView.pageControl = pageControl; pageControl.enabled = NO; pageControl.currentPageIndicatorTintColor = currentPageIndicatorTintColor; pageControl.pageIndicatorTintColor = pageIndicatorTintColor; pageControl.numberOfPages = scrollView.images.count; pageControl.bounds = CGRectMake(0, 0, scrollView.bounds.size.width, 44); pageControl.center = CGPointMake(scrollView.bounds.size.width * 0.5, scrollView.bounds.size.height * 0.9); [scrollView addSubview:pageControl]; [superView addSubview:scrollView]; //=************************ stone *** return scrollView; } @end
以上就是本文的全體內容,願望對年夜家的進修有所贊助。
【Objective-C完成無窮輪回輪播器】的相關資料介紹到這裡,希望對您有所幫助! 提示:不會對讀者因本文所帶來的任何損失負責。如果您支持就請把本站添加至收藏夾哦!