01.轮播图之三 : collectionView 轮播
生活随笔
收集整理的這篇文章主要介紹了
01.轮播图之三 : collectionView 轮播
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
個人覺得 collection view 做輪播是最方便的,設置下flowlayout 其他不會有很大的變動,沒有什么邏輯的代碼
let's begin……
創建自定義的view
.h 聲明文件
@interface CollectionViewShuffling : UIView@property (nonatomic,strong)NSArray *array;@end.m 實現文件
@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; } /**這個是橫向滾動輪播的重點研究對象
*/ -( UICollectionViewFlowLayout *)creatFlowLayout{// 創建UICollectionViewFlowLayout約束對象UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];// 設置item的Size大小flowLayout.itemSize = CGSizeMake(self.frame.size.width, self.frame.size.height);// 設置uicollection 的 橫向滑動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];// 設置代理_collectionView.delegate = self;_collectionView.dataSource = self; // _collectionView.showsHorizontalScrollIndicator = NO;// 設置不展示滑動條_collectionView.pagingEnabled = YES; // 設置整頁滑動// 設置當前collectionView 到哪個位置(indexPath row 0 section 取中間(50個)) [_collectionView registerNib:[UINib nibWithNibName:@"ShufflingItem" bundle:nil] forCellWithReuseIdentifier:@"ShufflingItem"];}return _collectionView; }-(void)setArray:(NSArray *)array{NSAssert(array.count != 0, @"傳入的滾動數組是 空的");_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];// 首位 添加數組最后的元素 [self.collectionArray addObject:_array.lastObject];// 添加數組元素 [self.collectionArray addObjectsFromArray:_array];// 末尾 補充第一個元素 [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));//檢測移動的位移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{// 正常滾動 }} }
?最簡單的collection 輪播,實現啦……
總結下,他的實現為什么如此簡單
調用方法::::
-(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; }?
這個寫完,距離成功又進了一步,繼續………………
轉載于:https://www.cnblogs.com/Bob-blogs/p/6773070.html
總結
以上是生活随笔為你收集整理的01.轮播图之三 : collectionView 轮播的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: NS方程由精确解求源项matlab代码
- 下一篇: 记一次被动的网卡升级:VMWare导致的