欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

iOS中無限循環(huán)滾動(dòng)簡單處理實(shí)現(xiàn)原理分析

 更新時(shí)間:2017年12月19日 15:40:24   作者:Little_Dad  
這篇文章主要介紹了iOS中無限循環(huán)滾動(dòng)簡單處理實(shí)現(xiàn)原理分析,需要的朋友可以參考下

說下原理:

1./*初始化/

+ (instancetype)loopScrollViewWithFrame:(CGRect)frame;

將背景collectinview視圖初始化設(shè)置 代理和數(shù)據(jù)源 、 布局

2.在激活initwithFrame后觸發(fā) layoutSubviews

 //默認(rèn)滾動(dòng)到要顯示的第一張圖片
 if (self.imageCollectionView.contentOffset.x == 0) {
  NSIndexPath *indexPath = [NSIndexPath indexPathForItem:1 inSection:0];
  [self scrollToIndexPath:indexPath animated:NO];
  self.currentIndex = 1;
}

界面展示出來的時(shí)候默認(rèn) 顯示 真實(shí)下標(biāo)也就是從1開始

設(shè)置真實(shí)數(shù)據(jù)源 imageList ,然后展示 的 數(shù)據(jù)源是loopImageList 這里 呢 多出2個(gè)對(duì)象,0和末尾,設(shè)置時(shí) 最后 和 起始,setImageList如下

- (void)setImageList:(NSMutableArray *)imageList {
 _imageList = imageList;
 self.loopImageList = [NSMutableArray arrayWithArray:imageList];
 if (imageList.count>0) {
  [self.loopImageList insertObject:[imageList lastObject] atIndex:0];
  [self.loopImageList addObject:[imageList objectAtIndex:0]];
 }
}

核心代碼和思路

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
 CGFloat width = self.bounds.size.width;
 //在loopImageList中,有n+2個(gè)對(duì)象,因此index取offset.x/width后的整數(shù)
 NSInteger index = scrollView.contentOffset.x/width;
 //這個(gè)比值很重要
 CGFloat ratio = scrollView.contentOffset.x/width;
 //從顯示的最后一張往后滾,自動(dòng)跳轉(zhuǎn)到顯示的第一張
 if (index == self.loopImageList.count-1) {
  self.currentIndex = 1;
  NSIndexPath *indexPath = [NSIndexPath indexPathForItem:self.currentIndex inSection:0];
  [self scrollToIndexPath:indexPath animated:NO];
  return;
 }
 //從顯示的第一張往前滾,自動(dòng)跳轉(zhuǎn)到顯示的最后一張
 //這里判斷條件為contentOffset.x和寬的比值,在往前滾快要結(jié)束的時(shí)候,能達(dá)到無縫切換到顯示的最后一張的效果
 if (ratio <= 0.01) {
  self.currentIndex = self.imageList.count;
  NSIndexPath *indexPath = [NSIndexPath indexPathForItem:self.currentIndex inSection:0];
  [self scrollToIndexPath:indexPath animated:NO];
  return;
 }
 if (self.currentIndex != index) {
  self.currentIndex = index;
 }
 NSLog(@"currentIndex = %ld",self.currentIndex);
}

這里的原因是為什么呢?

這時(shí)候在圖滾動(dòng) 執(zhí)行代理 監(jiān)聽的時(shí)候 ,我們的collectionview有設(shè)置 pageEnable 分頁屬性很關(guān)鍵有分頁動(dòng)畫。

當(dāng)偏移量判斷 真實(shí)的數(shù)據(jù)顯示到了最后一張。也就是8 滾到1的時(shí)候 ,設(shè)置回滾 ,回到默認(rèn)位置,且沒有動(dòng)畫。

另外一步處理當(dāng)偏移量 小于 一個(gè)極小值 也就是 偏移即將到達(dá) 0 的是偶也就是 真實(shí)的第一張回滾到最后 一張的時(shí)候,設(shè)置默認(rèn)滾動(dòng)到最后一張。

最重要的一點(diǎn) 這個(gè)黑科技 是使用scro 滾動(dòng)到特定的item所以 在觸發(fā)的那一時(shí)刻,item就設(shè)定死了,scrollViewDidScroll:也就不會(huì)再滾動(dòng),因?yàn)楝F(xiàn)在的偏移量是一個(gè)唯一值。

總結(jié)

以上所述是小編給大家介紹的iOS中無限循環(huán)滾動(dòng)簡單處理實(shí)現(xiàn)原理分析,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!

相關(guān)文章

最新評(píng)論