1、屬性oop
咱們須要一個 collectionView 和一個 NStimer 。collectionView 用來存放須要循環播放的對象, NSTimer 用來定時滾動collectionViewatom
@interface ViewController ()<UICollectionViewDelegate,UICollectionViewDataSource> @property(nonatomic,strong)UICollectionView* showCollection; @property(nonatomic,strong)NSTimer* timer; @end
提示:須要用到UISCrollView 或者 UICollectionView,咱們能夠直接遵照其代理、數據源。spa
2、初始化對象代理
初始化collectionView:code
#pragma mark - 初始化collectionView -(void)initCollectionView{ //layout UICollectionViewFlowLayout* layout = [[UICollectionViewFlowLayout alloc]init]; self.showCollection = [[UICollectionView alloc]initWithFrame:CGRectMake(120, 120, 200, 180) collectionViewLayout:layout]; _showCollection.delegate = self; _showCollection.dataSource = self; [self.view addSubview:_showCollection]; //基本設置 _showCollection.pagingEnabled = YES; _showCollection.showsHorizontalScrollIndicator = NO; layout.itemSize = CGSizeMake(200, 180); layout.scrollDirection = UICollectionViewScrollDirectionHorizontal; layout.minimumLineSpacing = 0; layout.minimumInteritemSpacing = 0; }
添加timer:orm
#pragma mark - 添加timer -(void)addTimer{ self.timer = [NSTimer scheduledTimerWithTimeInterval:1.5 target:self selector:@selector(nextPage) userInfo:nil repeats:YES]; [[NSRunLoop mainRunLoop]addTimer:_timer forMode:NSRunLoopCommonModes]; } -(void)nextPage{ //當前偏移量 CGPoint current = _showCollection.contentOffset; if (current.x == _showCollection.frame.size.width*6) { current.x = 0; }else{ current.x += _showCollection.frame.size.width; } //UIScrollView自帶方法 [_showCollection setContentOffset:current animated:YES]; }
提示:NSTimer須要加入到運行循環對象
3、數據源方法blog
實現數據源方法:此處設置section爲1,有六張圖片設置item爲7,最後一個item放跟第一張圖片同樣的,這樣就能夠給用戶一種假象:用戶會覺得是第一張,咱們在這個地方須要修改偏移量offset(在後邊會上代碼)圖片
#pragma mark - 數據源方法 - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{ return 7; } - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{ ShowCollectionViewCell* cell = [collectionView dequeueReusableCellWithReuseIdentifier:key forIndexPath:indexPath]; cell.backgroundColor = [UIColor redColor]; cell.icon = [NSString stringWithFormat:@"%zd",indexPath.item+1]; return cell; }
注意:自定義咱們須要的cell,須要註冊,」key「最好static一個靜態的ci
- (void)viewDidLoad { [super viewDidLoad]; //建立 [self initCollectionView]; //註冊 [_showCollection registerClass:[ShowCollectionViewCell class] forCellWithReuseIdentifier:key]; //添加timer [self addTimer]; }
4、代理方法
1,用戶本身查看的時候須要暫停timer;二、用戶中止查看,從新開啓timer;三、最後一個的時候,設置從第一個從新開始
#pragma mark - 代理方法 -(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{ [_timer invalidate]; } -(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{ [self addTimer]; } -(void)scrollViewDidScroll:(UIScrollView *)scrollView{ //所在頁數 int pageNumber = _showCollection.contentOffset.x/_showCollection.frame.size.width; //若是是最後一頁,設置偏移量爲0,從新循環 if (pageNumber == 6) { _showCollection.contentOffset = CGPointMake(0, 0); } }
5、自定義collectionViewCell
在這裏直接粘代碼,開發中根據不一樣的需求自定義
在collectionViewCell.h文件中:
@property(nonatomic,copy)NSString* icon;
在collectionViewCell.m文件中:實現初始化方法,setter方法
#import "ShowCollectionViewCell.h" @interface ShowCollectionViewCell() @property(nonatomic,strong)UIImageView* imageView; @end @implementation ShowCollectionViewCell -(instancetype)initWithFrame:(CGRect)frame{ self = [super initWithFrame:frame]; if (self) { _imageView = [[UIImageView alloc]init]; [self addSubview:_imageView]; } return self; } -(void)setIcon:(NSString *)icon{ _icon = [icon copy]; _imageView.image = [UIImage imageNamed:icon]; } -(void)layoutSubviews{ [super layoutSubviews]; _imageView.frame = self.bounds; } @end
注意點:一、初始化collectionViewCell別忘記註冊
二、跟tableView不一樣的是,咱們只須要寫出重用cell 的代碼,系統會建立
以上就能夠寫出一個簡單實用的輪播器了。
初入博客園,你們多多關照。