国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > iOS > 正文

iOS實現自動循環播放的banner實例詳解

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

前言

對于banner輪播圖,相信大家都會經常用到。自動循環播放的banner是很常見的UI組件。如何實現呢?下面就來給大家詳細介紹下,話不多說了,下面來一起學習學習吧。

1.實現思路

1.橫向滾動的banner。

  • UIScrollViw+UIImageView.
  • UICollectionView+UICollectionViewCell.
  • 前者需要自己做重用UIImageView,后者可以直接重用UICollectionViewCell。如果前者沒有做重用,多占用內存。

2.自動循環播放banner。

  • 可以使用計時器觸發循環播放.
  • 拖動或手動滑動banner時,停止自動循環播放banner。手勢停止后,開啟自動循環播放banner。

3.特殊banner位的處理。

  • 處于第1個或最后1個時,為保證橫向自動滑動效果流暢性,不跳動,需要特殊處理下。
  • 在生成banner時,第1個前面插入最后1個banner。最后1個banner后面插入第1個banner。當滑動到最后1個banner時,重置于第2個banner位。

2.本文采用第二種:UICollectionView+UICollectionViewCell

關鍵代碼實現

2.1生成banner的特殊處理

- (void)setBannerList:(NSArray<KBannerItem *> *)bannerList { if (bannerList.count > 1) {  NSMutableArray *itemList = [NSMutableArray arrayWithArray:bannerList];  [itemList insertObject:bannerList.lastObject atIndex:0];  [itemList addObject:bannerList.firstObject];  _bannerList = itemList; }else{  _bannerList = bannerList; } if (self.bannerList.count > 1) {  self.bannerPageControl.numberOfPages = self.bannerList.count - 2; }else{  self.bannerPageControl.numberOfPages = 0; } self.noBannerImageView.hidden = self.bannerList.count > 0; self.bannerPageControl.currentPage = 0; [self.collectionView reloadData]; self.collectionView.contentOffset = CGPointMake(CGRectGetWidth(self.collectionView.frame), 0);}

2.2 banner自動循環播放觸發的事件

- (void)handleBannerChangeEvent:(id)sender { if (_bannerPageControl.numberOfPages <= 1) {  return; } NSInteger page = _bannerPageControl.currentPage + 1; [self.collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForRow:page + 1 inSection:0] atScrollPosition:UICollectionViewScrollPositionRight animated:YES];}

2.3開啟自動播放或關閉自動播放bannner。

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView { self.countTimer.isOpen = NO;}- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView     willDecelerate:(BOOL)decelerate { self.countTimer.isOpen = YES;}

2.4.滑動時的特殊處理。

- (void)scrollViewDidScroll:(UIScrollView *)scrollView { if (_bannerList.count <=1) {  return; } CGFloat width = CGRectGetWidth(scrollView.frame); NSInteger currentPage = scrollView.contentOffset.x / width; if (currentPage == 0) {  if (scrollView.contentOffset.x < 0) {   [self.collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForRow:_bannerList.count - 2 inSection:0] atScrollPosition:UICollectionViewScrollPositionRight animated:NO];   self.bannerPageControl.currentPage = _bannerList.count - 2;  }else{   [self.collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForRow:1 inSection:0] atScrollPosition:UICollectionViewScrollPositionRight animated:NO];   self.bannerPageControl.currentPage = 0;  } }else if (currentPage == _bannerList.count - 1) {  self.bannerPageControl.currentPage = 0;  [self.collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForRow:1 inSection:0] atScrollPosition:UICollectionViewScrollPositionRight animated:NO]; }else{  self.bannerPageControl.currentPage = currentPage - 1; }}

總結

以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作具有一定的參考學習價值,如果有疑問大家可以留言交流,謝謝大家對武林網的支持。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 得荣县| 电白县| 库尔勒市| 广州市| 龙陵县| 行唐县| 九龙坡区| 旺苍县| 锦屏县| 台南县| 罗定市| 宣城市| 黄平县| 德格县| 阿克| 漠河县| 漳浦县| 那曲县| 鄂温| 崇左市| 界首市| 永年县| 中西区| 上犹县| 镇赉县| 红安县| 青河县| 比如县| 虎林市| 涪陵区| 托克逊县| 郓城县| 巫山县| 揭西县| 合水县| 永胜县| 长兴县| 炉霍县| 多伦县| 梅州市| 阿坝县|