激情久久久_欧美视频区_成人av免费_不卡视频一二三区_欧美精品在欧美一区二区少妇_欧美一区二区三区的

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術(shù)|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務(wù)器之家 - 編程語言 - IOS - IOS使用UICollectionView實現(xiàn)無限輪播效果

IOS使用UICollectionView實現(xiàn)無限輪播效果

2021-01-12 16:06yixiangboy IOS

這篇文章主要為大家詳細(xì)介紹了IOS使用UICollectionView實現(xiàn)無限輪播效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下

一、案例演示

本案例demo演示的是一個首頁輪播的案例,支持手動輪播和自動輪播。知識點主要集中在uicollectionview和nstimer的使用。

IOS使用UICollectionView實現(xiàn)無限輪播效果

二、知識儲備

2.1、uicollectionview橫向布局

只需要設(shè)置uicollectionviewflowlayout的scrolldirection為uicollectionviewscrolldirectionhorizontal即可。

2.2、nstimer的基本使用

nstimer的初始化:

 

復(fù)制代碼 代碼如下:
 + (nstimer *)scheduledtimerwithtimeinterval:(nstimeinterval)ti target:(id)atarget selector:(sel)aselector userinfo:(nullable id)userinfo repeats:(bool)yesorno;

 

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ā)送一次就失效。
開啟定時器:

 

復(fù)制代碼 代碼如下:
[[nsrunloop mainrunloop] addtimer:timer formode:nsrunloopcommonmodes];

 

關(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í)有所幫助。

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 成人午夜高清 | 久久久久久久久久性 | 黄色影院在线 | 成人国产精品一区二区毛片在线 | 久久网页 | 精品三级内地国产在线观看 | 桥本有菜免费av一区二区三区 | av不卡毛片| 欧美亚洲一区二区三区四区 | 久久久亚洲欧美综合 | 国产资源在线播放 | 亚洲人成网站免费播放 | 免费视频一区 | 全黄裸片武则天一级第4季 九色p | 精品一区二区三区在线视频 | 欧美日韩精品不卡一区二区三区 | 欧美在线观看视频网站 | 91网页| 日本aaaa片毛片免费观蜜桃 | 国产一级在线看 | 欧洲精品久久久久69精品 | 日韩激情一区 | 国产一区二区在线免费观看 | 黄色片快播 | 欧美人一级淫片a免费播放 久久99精品久久久久久园产越南 | 大片毛片 | 国产免费视频在线 | 久久精品久久久久 | 成人激情在线观看 | 久久一区三区 | 日本黄色一级视频 | 性插视频| 久久精品久久精品国产大片 | 日本成人二区 | 92自拍视频 | 强伦女教师视频 | 国内精品久久久久久久久久 | 激情小说色 | 亚洲第五色综合网 | 久久久av亚洲男天堂 | 国产精品美女一区二区 |