日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

01.轮播图之三 : collectionView 轮播

發布時間:2023/12/20 编程问答 38 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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 輪播,實現啦……

總結下,他的實現為什么如此簡單

  • collection view 有個flow layout 設置這個屬性就可以讓他橫向滾動,豎向滾動
  • 還有一個重點 聲明下 collection view 使用 item cell 的時候,是必須注冊的
  • 調用方法::::

    -(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 轮播的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。