01.轮播图之三 : collectionView 轮播
生活随笔
收集整理的這篇文章主要介紹了
01.轮播图之三 : collectionView 轮播
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
個(gè)人覺(jué)得 collection view 做輪播是最方便的,設(shè)置下flowlayout 其他不會(huì)有很大的變動(dòng),沒(méi)有什么邏輯的代碼
let's begin……
創(chuàng)建自定義的view
.h 聲明文件
@interface CollectionViewShuffling : UIView@property (nonatomic,strong)NSArray *array;@end.m 實(shí)現(xiàn)文件
@interface CollectionViewShuffling ()<UICollectionViewDelegate, UICollectionViewDataSource>@property (nonatomic,strong)UICollectionView *collectionView; @property (nonatomic,strong)NSMutableArray *collectionArray;@end@implementation CollectionViewShuffling @synthesize array = _array;-(instancetype)initWithFrame:(CGRect)frame{if (self == [super initWithFrame:frame]) {}return self; } /**這個(gè)是橫向滾動(dòng)輪播的重點(diǎn)研究對(duì)象
*/ -( UICollectionViewFlowLayout *)creatFlowLayout{// 創(chuàng)建UICollectionViewFlowLayout約束對(duì)象UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];// 設(shè)置item的Size大小flowLayout.itemSize = CGSizeMake(self.frame.size.width, self.frame.size.height);// 設(shè)置uicollection 的 橫向滑動(dòng)flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;flowLayout.minimumLineSpacing = 0;return flowLayout; }- (UICollectionView *)collectionView {if (!_collectionView) {UICollectionViewFlowLayout *flowLayout = [self creatFlowLayout];_collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height) collectionViewLayout:flowLayout];[self addSubview:_collectionView];// 設(shè)置代理_collectionView.delegate = self;_collectionView.dataSource = self; // _collectionView.showsHorizontalScrollIndicator = NO;// 設(shè)置不展示滑動(dòng)條_collectionView.pagingEnabled = YES; // 設(shè)置整頁(yè)滑動(dòng)// 設(shè)置當(dāng)前collectionView 到哪個(gè)位置(indexPath row 0 section 取中間(50個(gè))) [_collectionView registerNib:[UINib nibWithNibName:@"ShufflingItem" bundle:nil] forCellWithReuseIdentifier:@"ShufflingItem"];}return _collectionView; }-(void)setArray:(NSArray *)array{NSAssert(array.count != 0, @"傳入的滾動(dòng)數(shù)組是 空的");_array = array;[self prepareData];[self prepareUI]; }-(void)prepareUI{[self.collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:1 inSection:0] atScrollPosition:UICollectionViewScrollPositionLeft animated:false];[self.collectionView reloadData]; }- (void)prepareData{self.collectionArray = [NSMutableArray new];// 首位 添加數(shù)組最后的元素 [self.collectionArray addObject:_array.lastObject];// 添加數(shù)組元素 [self.collectionArray addObjectsFromArray:_array];// 末尾 補(bǔ)充第一個(gè)元素 [self.collectionArray addObject:_array.firstObject]; } /*collection view delegate*/ - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {return 1; } - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {return self.collectionArray.count; }- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {ShufflingItem *item = [collectionView dequeueReusableCellWithReuseIdentifier:@"ShufflingItem" forIndexPath:indexPath];if (!item) {item = [[ShufflingItem alloc] init];}item.imageView.backgroundColor = self.collectionArray[indexPath.row];return item; }-(void)scrollViewDidScroll:(UIScrollView *)scrollView{if (scrollView == self.collectionView) {NSLog(@"scroll content %@",NSStringFromCGPoint(scrollView.contentOffset));//檢測(cè)移動(dòng)的位移if (scrollView.contentOffset.x == (self.collectionArray.count-1)*(self.frame.size.width) ) {[self.collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:1 inSection:0] atScrollPosition:UICollectionViewScrollPositionLeft animated:false];}else if (scrollView.contentOffset.x == 0){[self.collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:(self.collectionArray.count-2) inSection:0] atScrollPosition:UICollectionViewScrollPositionLeft animated:false];}else{// 正常滾動(dòng) }} }
?最簡(jiǎn)單的collection 輪播,實(shí)現(xiàn)啦……
總結(jié)下,他的實(shí)現(xiàn)為什么如此簡(jiǎn)單
調(diào)用方法::::
-(void)prepareCollectShuffling{CollectionViewShuffling *collectShuffling = [[CollectionViewShuffling alloc]initWithFrame:CGRectMake(10, 320, self.view.frame.size.width -20, 220)];[self.view addSubview:collectShuffling];;collectShuffling.array = self.arr; }?
這個(gè)寫完,距離成功又進(jìn)了一步,繼續(xù)………………
轉(zhuǎn)載于:https://www.cnblogs.com/Bob-blogs/p/6773070.html
總結(jié)
以上是生活随笔為你收集整理的01.轮播图之三 : collectionView 轮播的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: NS方程由精确解求源项matlab代码
- 下一篇: 记一次被动的网卡升级:VMWare导致的