亚洲香蕉成人av网站在线观看_欧美精品成人91久久久久久久_久久久久久久久久久亚洲_热久久视久久精品18亚洲精品_国产精自产拍久久久久久_亚洲色图国产精品_91精品国产网站_中文字幕欧美日韩精品_国产精品久久久久久亚洲调教_国产精品久久一区_性夜试看影院91社区_97在线观看视频国产_68精品久久久久久欧美_欧美精品在线观看_国产精品一区二区久久精品_欧美老女人bb

首頁 > 系統 > iOS > 正文

一行iOS代碼實現圖片無限輪播器

2020-07-26 02:48:36
字體:
來源:轉載
供稿:網友

最近一直在找實現圖片無限輪播的方法,在網上也看了不少方法,大都不太合適,最終看到某IT培訓公司一位講師用

UICollectionView:一行代碼實現圖片無限輪播器的方法,當然想一行代碼實現輪播功能,前期還是有一些工作要做。下面就把這個方法分享給大家!

一、圖片無限輪播實現效果圖:

圖片無限輪播.gif

二、實現原理與分析:

假設有三張圖片0、1、2,想要實現無限輪播,我們可以將UICollectionView的cell個數設為圖片的個數 x 3,也就是把三張圖片重復添加到9個cell中,可以把無限輪播分解成五種特殊的狀態(五個臨界點),輪播開始時為初始狀態,在定時器的作用下依次滾動到最后一個cell,此時為右臨界狀態顯示的是第2張圖片,若想繼續無縫滾動到第0圖片,我們可以偷偷的將collectionView滾動到第三個cell上,可以看第四幅轉態圖此時顯示的依然是第2張圖片,這樣再次滾動就是第0張圖,這樣就實現了cell向左滾動的無限循環輪播;向右滾動的原理一樣,就是第三幅圖到第五幅圖的變化。

初始界狀態.png

右臨界狀態.png

左臨界狀態.png

Paste_Image.png

Paste_Image.png

三、代碼:

類文件.png

  •  JFWeakTimerTargetObject繼承自NSObject
  • JFLoopView繼承自UIView
  • JFLoopViewCell繼承自UICollectionViewCell
  • JFLoopViewLayout繼承自UICollectionViewFlowLayout
  • JFMainViewController繼承自UIViewController

JFWeakTimerTargetObject重寫定時器NSTimer的+ (NSTimer *)scheduledTimerWithTimeInterval:(NSTimeInterval)ti target:(id)aTarget selector:(SEL)aSelector userInfo:(nullable id)userInfo repeats:(BOOL)yesOrNo;類方法的目的是:避免當定時器強引用JFLoopView類,JFLoopView無法被釋放的問題。

JFWeakTimerTargetObject.h文件

#import <Foundation/Foundation.h>@interface JFWeakTimerTargetObject : NSObject+ (NSTimer *)scheduledTimerWithTimeInterval:(NSTimeInterval)ti target:(id)aTarget selector:(SEL)aSelector userInfo:(nullable id)userInfo repeats:(BOOL)yesOrNo;@end

JFWeakTimerTargetObject.m文件

#import "JFWeakTimerTargetObject.h"@interface JFWeakTimerTargetObject ()@property (nonatomic, weak) id target;@property (nonatomic, assign) SEL selector;@end@implementation JFWeakTimerTargetObject+ (NSTimer *)scheduledTimerWithTimeInterval:(NSTimeInterval)ti target:(id)aTarget selector:(SEL)aSelector userInfo:(nullable id)userInfo repeats:(BOOL)yesOrNo { //創建當前類對象 JFWeakTimerTargetObject *object = [[JFWeakTimerTargetObject alloc] init]; object.target = aTarget; object.selector = aSelector; return [NSTimer scheduledTimerWithTimeInterval:ti target:object selector:@selector(fire:) userInfo:userInfo repeats:yesOrNo];}- (void)fire:(id)obj { [self.target performSelector:self.selector withObject:obj];}@end

JFLoopView.h文件

#import <UIKit/UIKit.h>@interface JFLoopView : UIView//JFLoopView初始化方法- (instancetype)initWithImageArray:(NSArray *)imageArray;@end

JFLoopView.m文件

#import "JFLoopView.h"#import "JFLoopViewLayout.h"#import "JFLoopViewCell.h"#import "JFWeakTimerTargetObject.h"@interface JFLoopView () <UICollectionViewDelegate, UICollectionViewDataSource>@property (nonatomic, strong) UICollectionView *collectionView;@property (nonatomic, strong) UIPageControl *pageControl;@property (nonatomic, strong) NSArray *imageArray;@property (nonatomic, weak) NSTimer *timer;@endstatic NSString *ID = @"loopViewCell";@implementation JFLoopView- (instancetype)initWithImageArray:(NSArray *)imageArray { if (self = [super init]) { UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:[[JFLoopViewLayout alloc] init]]; [collectionView registerClass:[JFLoopViewCell class] forCellWithReuseIdentifier:ID]; collectionView.dataSource = self; collectionView.delegate = self; [self addSubview:collectionView]; self.collectionView = collectionView; self.imageArray = imageArray; //添加分頁器 [self addSubview:self.pageControl]; //回到主線程刷新UI dispatch_async(dispatch_get_main_queue(), ^{ //設置滾動的初始狀態在 [self.collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:self.imageArray.count inSection:0] atScrollPosition:UICollectionViewScrollPositionLeft animated:NO]; //添加定時器 [self addTimer]; }); } return self;}/// 懶加載pageControl- (UIPageControl *)pageControl { if (!_pageControl) { _pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(0, 220, 0, 30)]; _pageControl.numberOfPages = self.imageArray.count; _pageControl.pageIndicatorTintColor = [UIColor orangeColor]; _pageControl.currentPageIndicatorTintColor = [UIColor purpleColor]; } return _pageControl;}#pragma mark --- UICollectionViewDataSource 數據源方法- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section { return self.imageArray.count * 3;}- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { JFLoopViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:ID forIndexPath:indexPath]; cell.imageName = self.imageArray[indexPath.item % self.imageArray.count]; return cell;}#pragma mark ---- UICollectionViewDelegate/// 滾動完畢就會調用(如果不是人為拖拽scrollView導致滾動完畢,才會調用這個方法)- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView { [self scrollViewDidEndDecelerating:scrollView];}/// 當滾動減速時調用- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView { CGFloat offsetX = scrollView.contentOffset.x; NSInteger page = offsetX / scrollView.bounds.size.width; //手動滾動到左邊臨界狀態 if (page == 0) { page = self.imageArray.count; self.collectionView.contentOffset = CGPointMake(page * scrollView.frame.size.width, 0); //滾動到右臨界狀態 }else if (page == [self.collectionView numberOfItemsInSection:0] - 1) { page = self.imageArray.count - 1; self.collectionView.contentOffset = CGPointMake(page * scrollView.frame.size.width, 0); } //設置UIPageControl當前頁 NSInteger currentPage = page % self.imageArray.count; self.pageControl.currentPage =currentPage; //添加定時器 [self addTimer];}///手指開始拖動時調用- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView { //移除定時器 [self removeTimer];}/// 添加定時器- (void)addTimer { if (self.timer) return; self.timer = [JFWeakTimerTargetObject scheduledTimerWithTimeInterval:1.5 target:self selector:@selector(nextImage) userInfo:nil repeats:YES]; [[NSRunLoop currentRunLoop] addTimer:self.timer forMode:NSRunLoopCommonModes];}/// 移除定時器- (void)removeTimer { [self.timer invalidate]; self.timer = nil;}/// 切換到下一張圖片- (void)nextImage { CGFloat offsetX = self.collectionView.contentOffset.x; NSInteger page = offsetX / self.collectionView.bounds.size.width; [self.collectionView setContentOffset:CGPointMake((page + 1) * self.collectionView.bounds.size.width, 0) animated:YES];}- (void)layoutSubviews { [super layoutSubviews]; self.collectionView.frame = self.bounds;}- (void)dealloc { [self removeTimer];}@end

JFLoopViewCell.h文件

#import <UIKit/UIKit.h>@interface JFLoopViewCell : UICollectionViewCell@property (nonatomic, copy) NSString *imageName;@end

JFLoopViewCell.m文件

#import "JFLoopViewCell.h"@interface JFLoopViewCell ()@property (nonatomic, weak) UIImageView *iconView;@end@implementation JFLoopViewCell- (instancetype)initWithFrame:(CGRect)frame { if (self = [super initWithFrame:frame]) { UIImageView *iconView = [[UIImageView alloc] init]; [self addSubview:iconView]; self.iconView = iconView; } return self;}- (void)setImageName:(NSString *)imageName { _imageName = imageName; self.iconView.image = [UIImage imageNamed:imageName];}- (void)layoutSubviews { [super layoutSubviews]; self.iconView.frame = self.bounds;}@end

JFLoopViewLayout.h文件

#import <UIKit/UIKit.h>@interface JFLoopViewLayout : UICollectionViewFlowLayout@end

JFLoopViewLayout.m文件

#import "JFLoopViewLayout.h"@implementation JFLoopViewLayout/// 準備布局- (void)prepareLayout { [super prepareLayout]; //設置item尺寸 self.itemSize = self.collectionView.frame.size; //設置滾動方向 self.scrollDirection = UICollectionViewScrollDirectionHorizontal; //設置分頁 self.collectionView.pagingEnabled = YES; //設置最小間距 self.minimumLineSpacing = 0; self.minimumInteritemSpacing = 0; //隱藏水平滾動條 self.collectionView.showsHorizontalScrollIndicator = NO;}@end

JFMainViewController.h文件

#import <UIKit/UIKit.h>@interface JFMainViewController : UIViewController@end

JFMainViewController.m文件

#import "JFMainViewController.h"#import "JFLoopView.h"@interface JFMainViewController ()@property (nonatomic, strong) JFLoopView *loopView;@end@implementation JFMainViewController- (void)viewDidLoad { [super viewDidLoad]; //關閉自動調整滾動視圖 self.automaticallyAdjustsScrollViewInsets = NO;}- (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; self.navigationController.navigationBar.hidden = YES;}- (void)loadView { [super loadView]; //設置圖片數據 NSArray *imageArray = @[@"srcoll_01",@"srcoll_02",@"srcoll_03"]; //此行代碼實現無限輪播 _loopView = [[JFLoopView alloc] initWithImageArray:imageArray]; //設置loopView的frame _loopView.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 250); [self.view addSubview:self.loopView];}- (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated.}@end

注意:如果你的控制器有UINavigationBar,且隱藏了navigationBar,一定要記得設置self.automaticallyAdjustsScrollViewInsets = NO; automaticallyAdjustsScrollViewInsets是干嘛的呢?簡單點說就是automaticallyAdjustsScrollViewInsets根據所在界面的status bar、navigationbar、與tabbar的高度,自動調整scrollview的 inset,設置為NO,不讓viewController調整,我們自己修改布局即可。如果不設置為NO就可能出現下面的情況,自動滾動和拖動的時候imageView的位置會變化。

圖片無限輪播bug展示.gif

四、總結:

1、實現無限輪播器的主要控件是UICollectionView和UIPageControl,
2、封裝好工具類以后再使用時一行_loopView = [[JFLoopView alloc] initWithImageArray:imageArray];代碼,然后設置frame就可以復用無限輪播器。
3、合理切換圖片和圖片排列的方法,加上恰當地使用UICollectionView提供的代理方法就可以完美的實現無限輪播器。

寫在最后:

下一篇文章講用UICollectionView實現電商APP首頁的方法:


電商APP的首頁展示.gif

如果你有好的方法敬請分享,感謝你的閱讀!歡迎關注和評論!

源碼地址:鏈接: https://pan.baidu.com/s/1nv5FqZJ 密碼: qz3u

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林網。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
亚洲香蕉成人av网站在线观看_欧美精品成人91久久久久久久_久久久久久久久久久亚洲_热久久视久久精品18亚洲精品_国产精自产拍久久久久久_亚洲色图国产精品_91精品国产网站_中文字幕欧美日韩精品_国产精品久久久久久亚洲调教_国产精品久久一区_性夜试看影院91社区_97在线观看视频国产_68精品久久久久久欧美_欧美精品在线观看_国产精品一区二区久久精品_欧美老女人bb
久久精品国产成人精品| 成人欧美一区二区三区黑人| 国产精品美女www| 亚洲人成77777在线观看网| 国产精品免费福利| 亚洲一区二区三区在线免费观看| 日日摸夜夜添一区| 亚洲欧美综合精品久久成人| 亚洲欧美日本精品| 精品magnet| 国产精品专区一| 日韩精品免费看| 在线观看视频99| 综合久久五月天| 国产成人综合精品在线| 亚洲国产一区二区三区四区| 日本高清不卡的在线| 久久久国产视频| 日韩精品视频在线| 欧美贵妇videos办公室| 97在线视频观看| 欧美亚洲国产精品| 亚洲成av人乱码色午夜| 欧美一区亚洲一区| 国产视频自拍一区| 国产精品一区久久| 欧美成人免费全部| 综合国产在线视频| 色先锋资源久久综合5566| 中文字幕亚洲第一| 久久韩剧网电视剧| 色婷婷综合成人av| 美女av一区二区| 91精品在线影院| 91精品久久久久久久久中文字幕| 精品少妇v888av| 热久久美女精品天天吊色| 欧美精品在线观看| 久久人91精品久久久久久不卡| 亚洲精品91美女久久久久久久| 精品久久久在线观看| 7m精品福利视频导航| 久久久噜噜噜久久| 亚洲欧美制服综合另类| 国产视频亚洲视频| 国产精品午夜一区二区欲梦| 色综合老司机第九色激情| 青青草国产精品一区二区| 亚洲天堂色网站| 米奇精品一区二区三区在线观看| 日本韩国在线不卡| 欧美一级淫片丝袜脚交| 日韩精品在线第一页| 久久久久久久亚洲精品| 亚洲色图综合网| 最好看的2019的中文字幕视频| 亚洲欧美国产高清va在线播| 久久久精品影院| 国产成+人+综合+亚洲欧美丁香花| 亚洲女人初尝黑人巨大| 日韩成人在线视频| 亚洲色图激情小说| 日韩成人xxxx| 亚洲电影第1页| 九九热在线精品视频| 欧美在线免费看| 亚洲色图第三页| 国产精品一区二区3区| 欧美—级a级欧美特级ar全黄| 亚洲天堂av女优| 国产成一区二区| 欧美日韩亚洲视频| 精品视频在线观看日韩| 国产福利精品视频| 福利视频第一区| 国产精品视频永久免费播放| 亚洲精品网站在线播放gif| 成人国产精品久久久| 性欧美办公室18xxxxhd| 成人午夜在线视频一区| 亚洲成人免费在线视频| 欧美久久精品一级黑人c片| 日韩欧美中文字幕在线观看| 日韩成人久久久| 国产欧美一区二区白浆黑人| 久久久久九九九九| 精品国产视频在线| 亚洲美女av在线| 国产91精品黑色丝袜高跟鞋| 成人疯狂猛交xxx| 成人做爰www免费看视频网站| 欧美一区二粉嫩精品国产一线天| 日韩精品欧美国产精品忘忧草| 日韩av电影中文字幕| 国内精品模特av私拍在线观看| 欧美视频中文字幕在线| 亚洲欧美国产另类| 国产伦精品免费视频| 成人精品网站在线观看| 久久国产精品久久精品| 2018日韩中文字幕| 国模精品系列视频| 久久久最新网址| 91精品久久久久久久久久久久久久| 亚洲日本aⅴ片在线观看香蕉| 国产一区二区日韩精品欧美精品| 国产最新精品视频| 精品偷拍一区二区三区在线看| 青青草原一区二区| 超碰精品一区二区三区乱码| 欧洲成人免费视频| 色综合久久88色综合天天看泰| 狠狠色狠狠色综合日日小说| 欧美日韩国产中文字幕| 精品国产自在精品国产浪潮| 国产亚洲免费的视频看| 久久久国产成人精品| 精品人伦一区二区三区蜜桃免费| 欧美美女操人视频| 欧美激情视频在线免费观看 欧美视频免费一| 亚洲欧美日韩另类| 精品成人av一区| 久久亚洲精品毛片| 欧美另类老女人| 亚洲乱码av中文一区二区| 国产97在线|亚洲| 欧美电影在线观看| 国产精品无码专区在线观看| 免费99精品国产自在在线| 欧美成人午夜视频| 国产视频精品va久久久久久| 国产精品亚洲精品| 亚洲天堂网站在线观看视频| 亚洲曰本av电影| 国产成人综合久久| 国产91精品久久久久久| 亚洲国产99精品国自产| 亚洲精品xxxx| 国产精品永久免费| 日韩免费av在线| 国产在线视频91| 色噜噜狠狠狠综合曰曰曰| 91色中文字幕| 亚洲成人三级在线| 国产精品9999| 中文字幕欧美日韩精品| 亚洲2020天天堂在线观看| 欧美激情啊啊啊| 久久久99久久精品女同性| 亚洲人成在线一二| 成人黄色av免费在线观看| 精品福利免费观看| 国产精品偷伦免费视频观看的| 亚洲三级av在线| 欧美亚洲国产视频小说| 中文字幕国产精品久久| 亚洲第一天堂av| 欧美性猛交xxxx偷拍洗澡| 日韩免费观看av| 日韩激情视频在线播放| 成人在线小视频| 国产美女搞久久| 国产综合在线观看视频| 国产精品久久久久福利|