一、案例演示
本案例demo演示的是一個首頁輪播的案例,支持手動輪播和自動輪播。知識點主要集中在uicollectionview和nstimer的使用。
二、知識儲備
2.1、uicollectionview橫向布局
只需要設(shè)置uicollectionviewflowlayout的scrolldirection為uicollectionviewscrolldirectionhorizontal即可。
2.2、nstimer的基本使用
nstimer的初始化:
1)、(nstimeinterval)ti : 預(yù)訂一個timer,設(shè)置一個時間間隔。
表示輸入一個時間間隔對象,以秒為單位,一個>0的浮點類型的值,如果該值<0,系統(tǒng)會默認(rèn)為0.1。
2)、target:(id)atarget : 表示發(fā)送的對象,如self
3)、selector:(sel)aselector : 方法選擇器,在時間間隔內(nèi),選擇調(diào)用一個實例方法
4)、userinfo:(nullable id)userinfo : 需要傳參,可以為nil
5)、repeats:(bool)yesorno : 當(dāng)yes時,定時器會不斷循環(huán)直至失效或被釋放,當(dāng)no時,定時器會循環(huán)發(fā)送一次就失效。
開啟定時器:
關(guān)閉定時器:
[self.timer invalidate];
2.3、自動輪播和手動輪播的切換
初始化的時候,我們默認(rèn)開啟定時器,定時執(zhí)行切換到下一張圖片的函數(shù)。當(dāng)用戶觸摸到view的時候,我們則要關(guān)閉定時器,手動的進行uicollectionview的切換。當(dāng)用戶的手離開了view,我們要重新打開定時器,進行自動輪播的切換。
三、關(guān)鍵代碼分析
3.1、生成uicollectionviewflowlayout對象,設(shè)置他的滾動方向為水平滾動
1
2
3
4
|
uicollectionviewflowlayout *flowlayout = [[uicollectionviewflowlayout alloc] init]; flowlayout.itemsize = cgsizemake(screen_width, 200); flowlayout.scrolldirection = uicollectionviewscrolldirectionhorizontal; flowlayout.minimumlinespacing = 0; |
3.2、初始化uicollectionview對象
1
2
3
4
5
6
7
|
uicollectionview *collectionview = [[uicollectionview alloc] initwithframe:cgrectmake(0, self.navbarheight, screen_width, 200) collectionviewlayout:flowlayout]; collectionview.delegate = self; collectionview.datasource = self; collectionview.showshorizontalscrollindicator = no; collectionview.pagingenabled = yes; collectionview.backgroundcolor = [uicolor clearcolor]; [self.view addsubview:collectionview]; |
3.3、uicollectionview的uicollectionviewdatasource代理方法
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
#pragma mark- uicollectionviewdatasource -(nsinteger)numberofsectionsincollectionview:(uicollectionview *)collectionview{ return yymaxsections; } -(nsinteger)collectionview:(uicollectionview *)collectionview numberofitemsinsection:(nsinteger)section{ return self.newses.count; } -(uicollectionviewcell *)collectionview:(uicollectionview *)collectionview cellforitematindexpath:(nsindexpath *)indexpath{ yycell *cell = [collectionview dequeuereusablecellwithreuseidentifier:yyidcell forindexpath:indexpath]; if (!cell){ cell = [[yycell alloc] init]; } cell.news=self.newses[indexpath.item]; return cell; } |
3.4、定時器的開啟和關(guān)閉
1
2
3
4
5
6
7
8
9
10
11
12
13
|
#pragma mark 添加定時器 -( void ) addtimer{ nstimer *timer = [nstimer scheduledtimerwithtimeinterval:1 target:self selector:@selector(nextpage) userinfo:nil repeats:yes]; [[nsrunloop mainrunloop] addtimer:timer formode:nsrunloopcommonmodes]; self.timer = timer ; } #pragma mark 刪除定時器 -( void ) removetimer{ [self.timer invalidate]; self.timer = nil; } |
3.5、手動切換 和 自動輪播 的切換
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
-( void ) scrollviewwillbegindragging:(uiscrollview *)scrollview{ [self removetimer]; } #pragma mark 當(dāng)用戶停止的時候調(diào)用 -( void ) scrollviewdidenddragging:(uiscrollview *)scrollview willdecelerate:( bool )decelerate{ [self addtimer]; } #pragma mark 設(shè)置頁碼 -( void ) scrollviewdidscroll:(uiscrollview *)scrollview{ int page = ( int ) (scrollview.contentoffset.x/scrollview.frame.size.width+0.5)%self.newses.count; self.pagecontrol.currentpage =page; } |
3.6、自動輪播切換到下一個view的方法
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
-( void ) nextpage{ nsindexpath *currentindexpath = [[self.collectionview indexpathsforvisibleitems] lastobject]; nsindexpath *currentindexpathreset = [nsindexpath indexpathforitem:currentindexpath.item insection:yymaxsections/2]; [self.collectionview scrolltoitematindexpath:currentindexpathreset atscrollposition:uicollectionviewscrollpositionleft animated:no]; nsinteger nextitem = currentindexpathreset.item +1; nsinteger nextsection = currentindexpathreset.section; if (nextitem==self.newses.count) { nextitem=0; nextsection++; } nsindexpath *nextindexpath = [nsindexpath indexpathforitem:nextitem insection:nextsection]; [self.collectionview scrolltoitematindexpath:nextindexpath atscrollposition:uicollectionviewscrollpositionleft animated:yes]; } |
demo下載地址:https://github.com/yixiangboy/yxcollectionview
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助。