假設(shè)有5張圖片,分別是:12345诚亚,實(shí)現(xiàn)輪播圖
方法1:用scrollView加NSTimer實(shí)現(xiàn),思路:12345五張圖片午乓,實(shí)現(xiàn)輪播站宗,我添加兩張,變成5123451益愈,當(dāng)滑到最后一個(gè)1時(shí)梢灭,無(wú)動(dòng)畫(huà)位移回第一個(gè)1;當(dāng)?shù)怪?時(shí)腕唧,無(wú)動(dòng)畫(huà)回最后的5或辖。
難點(diǎn)在于:給定數(shù)組的個(gè)數(shù),及兩個(gè)邊界的判斷
方法2:用collectionView加NSTimer實(shí)現(xiàn)枣接,思路:12345五張圖片,對(duì)應(yīng)collectionView的1個(gè)section缺谴,即一個(gè)section有5個(gè)row但惶;至于有多少個(gè)section,盡量設(shè)置的大一些湿蛔,eg:100膀曾;(collectionView有重用機(jī)制)所以不用擔(dān)心內(nèi)存問(wèn)題。
難點(diǎn)在于:滑動(dòng)的邏輯處理阳啥;如果你把section設(shè)置的非常大添谊,就不用擔(dān)心倒著滑的問(wèn)題,畢竟不是每個(gè)人都那么閑察迟。
- (void)nextPage:(id)sender {
// 1. 得到當(dāng)前正在顯示的cell的indexPath斩狱,(只有一個(gè))
NSIndexPath *currentIndexPath = [[self.collectionView indexPathsForVisibleItems] lastObject];
// 2. 得到Y(jié)YMaxSections/2對(duì)應(yīng)的section的indexPath耳高,顯示此indexPath對(duì)應(yīng)的cell
NSIndexPath *currentIndexPathReset = [NSIndexPath indexPathForItem:currentIndexPath.item inSection:YYMaxSections/2];
[self.collectionView scrollToItemAtIndexPath:currentIndexPathReset atScrollPosition:UICollectionViewScrollPositionLeft animated:NO];
// 3. 如果當(dāng)前section的row未顯示完全,則row+1所踊,否則section+1泌枪,row置為0
NSInteger nextItem = currentIndexPathReset.item + 1;
NSInteger nextSection = currentIndexPathReset.section;
if (nextItem == self.dataArray.count) {
nextItem = 0;
nextSection++;
}
// 4. 位移顯示效果
NSIndexPath *nextIndexPath = [NSIndexPath indexPathForItem:nextItem inSection:nextSection];
[self.collectionView scrollToItemAtIndexPath:nextIndexPath atScrollPosition:UICollectionViewScrollPositionLeft animated:YES];
}
方法3:用scrollView加NSTimer實(shí)現(xiàn),12345秕岛,只用3個(gè)imageView碌燕,每次滑動(dòng)的時(shí)候,始終保證下一個(gè)是居中继薛,eg:512修壕,123,234遏考,當(dāng)你從2滑到3的時(shí)候慈鸠,結(jié)束后位移從123變?yōu)榈?34;
代碼:https://github.com/mokong/CircleView
注:代碼里只有前兩種方法诈皿,沒(méi)有第三種林束,大家可以自己百度第三種的實(shí)現(xiàn)代碼;