iOS開發UI篇 無限輪播(迴圈展示)

2021-09-06 17:23:17 字數 3357 閱讀 5707

ios開發ui篇—無限輪播(迴圈展示)

一、簡單說明

之前的程式還存在乙個問題,那就是不能迴圈展示,因為plist檔案中只有五個陣列,因此第乙個和最後乙個之後就沒有了,下面介紹處理這種迴圈展示問題的小技巧。

方法一:使用乙個for迴圈,迴圈200次,建立200*=1000個模型,且預設程式啟動後處在第100組的位置,向前有500個模型,向後也有500個模型,產生一種迴圈展示的假象。

**如下:

1//2

07-無限滾動(迴圈利用)4//

5//6//

7//89

#import

"yyviewcontroller.h"10

#import

"mjextension.h"11

#import

"yynews.h"12

#import

"yycell.h"13

14#define yyidcell @"cell"

1516

@inte***ce yyviewcontroller ()17 @property (weak, nonatomic) iboutlet uicollectionview *collectinview;

18 @property(nonatomic,strong)nsmutablearray *news;

19@end

2021

@implementation

yyviewcontroller

2223

#pragma mark-懶載入

24//

-(nsarray *)news

25//

29//

return _news;

30//

}31 -(nsmutablearray *)news

3239}40

return

_news;41}

4243 - (void

)viewdidload

4454

55#pragma mark- uicollectionviewdatasource

56//

一共多少組,預設為1組

57 -(nsinteger)numberofsectionsincollectionview:(uicollectionview *)collectionview

5861 -(nsinteger)collectionview:(uicollectionview *)collectionview numberofitemsinsection:(nsinteger)section

6265

66 -(uicollectionviewcell *)collectionview:(uicollectionview *)collectionview cellforitematindexpath:(nsindexpath *)indexpath

6773

74#pragma mark-uicollectionviewdelegate

75@end

列印檢視所處的索引(全程依然只建立了兩個cell):

說明:[self.collectinview scrolltoitematindexpath: atscrollposition: animated:]

//預設處於第0組的第500個模型的左邊

方法二:設定其有100組,那麼一共有100*5=500個模型。且設定預設處於第50組的索引為0處。

**如下:

1//2

07-無限滾動(迴圈利用)4//

5//6//

7//89

#import

"yyviewcontroller.h"10

#import

"mjextension.h"11

#import

"yynews.h"12

#import

"yycell.h"13

14#define yyidcell @"cell"

1516

@inte***ce yyviewcontroller ()17 @property (weak, nonatomic) iboutlet uicollectionview *collectinview;

18 @property(nonatomic,strong)nsarray *news;

19@end

2021

@implementation

yyviewcontroller

2223

#pragma mark-懶載入

24 -(nsarray *)news

2529

return

_news;30}

31//

-(nsmutablearray *)news

32//

39//}40

//return _news;

41//}42

43 - (void

)viewdidload

4456

57#pragma mark- uicollectionviewdatasource

58//

一共多少組,預設為1組

59 -(nsinteger)numberofsectionsincollectionview:(uicollectionview *)collectionview

6063 -(nsinteger)collectionview:(uicollectionview *)collectionview numberofitemsinsection:(nsinteger)section

6467

68 -(uicollectionviewcell *)collectionview:(uicollectionview *)collectionview cellforitematindexpath:(nsindexpath *)indexpath

6975

76#pragma mark-uicollectionviewdelegate

77@end

注意:上面的兩種方法都建立了大量的無用的模型,不太可取。且在實際開發中,建議模型的總數不要太大,因為在其內部需要遍歷計算所有控制項的frame。

如果模型數量太大,會占用資源。

改進建議:可以監聽手指在上面的滾動,當停止滾動的時候,又重新設定初始的中間位置。

iOS開發UI篇 無限輪播(功能完善)

文頂頂 ios開發ui篇 無限輪播 功能完善 一 自動滾動 新增並設定乙個定時器,每個2.0秒,就跳轉到下一條。獲取當前正在展示的位置。1 self addnstimer 2 34 void addnstimer 59 void nextpage 10列印檢視 實現步驟 1 新增並設定定時器 2 設...

iOS開發UI篇 無限輪播(功能完善)

ios開發ui篇 無限輪播 功能完善 一 自動滾動 新增並設定乙個定時器,每個2.0秒,就跳轉到下一條。獲取當前正在展示的位置。1 self addnstimer 2 34 void addnstimer 59 void nextpage 10列印檢視 實現步驟 1 新增並設定定時器 2 設定定時器...

iOS開發UI篇 無限輪播(新聞資料展示)

ios開發ui篇 無限輪播 新聞資料展示 一 實現效果 二 實現步驟 1.前期準備 1 匯入資料轉模型的第三方框架mjextension 2 向專案中新增儲存有 新聞 資料的plist檔案 3 匯入用到的素材 2.步驟和 1 新建乙個資料模型 該模型的 設計如下 yynews.件 1 2 yynew...