我通過(guò)網(wǎng)上資料和自己試驗(yàn),得出一下結(jié)論耽装,不對(duì)的地方還請(qǐng)指出愤炸,大家共同學(xué)習(xí)。先申明個(gè)人技術(shù)不好5粞佟9娓觥!
第一種方式
在你需要展示的數(shù)據(jù)基礎(chǔ)上前后各在增加一個(gè)數(shù)據(jù)挥萌,前面增加的數(shù)據(jù)與你數(shù)據(jù)最后一個(gè)為同一個(gè)绰姻,后面增加數(shù)據(jù)與你數(shù)據(jù)的第一個(gè)為同一個(gè)。例如一個(gè)圖片列表[image1, image2, image3, image4] 通過(guò)前后各增加一個(gè)數(shù)據(jù)得到[image4, image1, image2, image3, image4, image1] 引瀑。
在滑動(dòng)到第一個(gè)數(shù)據(jù)的時(shí)候狂芋,即index為0,這時(shí)要讓scrollview后臺(tái)滑到倒數(shù)第2張憨栽;同理帜矾,滑動(dòng)到最后一張翼虫,這時(shí)要讓scrollview后臺(tái)滑到第1張。
注意:這種方案的缺點(diǎn)是數(shù)據(jù)多了屡萤,性能很差珍剑,只適用小數(shù)據(jù)。
這里穿插個(gè)問(wèn)題死陆,我們實(shí)現(xiàn)scrollview的代理招拙,應(yīng)該在哪個(gè)代理里進(jìn)行判斷,滑動(dòng)呢措译?
- (void)scrollViewDidScroll:(UIScollView *)scrollView
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
通過(guò)自己試驗(yàn)别凤,在scrollViewDidEndDragging判斷很困難,也不好操作领虹;在scrollViewDidEndDecelerating很好操作规哪,但是當(dāng)滑到第一張或者最后一張,手指未離開(kāi)屏幕繼續(xù)滑到塌衰,就不會(huì)執(zhí)行該方法诉稍,所以也不會(huì)有滑到操作,這里你可以滑到速度快點(diǎn)試試最疆,如果要求不高杯巨,這里操作最方便,快捷肚菠;
如果需要滑動(dòng)很快舔箭,又要達(dá)到循環(huán)輪播的要求,我是在scrollViewDidScroll進(jìn)行的判斷蚊逢;可能這里執(zhí)行次數(shù)會(huì)很多,性能相對(duì)不是很好箫章,但是目前沒(méi)想到更好的辦法烙荷,有更好的辦法也請(qǐng)?zhí)岢觯瑢W(xué)習(xí)一下檬寂。
scrollview判斷滑到時(shí)機(jī)就大概說(shuō)這么一點(diǎn)终抽,這是我判斷的代碼
- (void) scrollViewDidScroll:(UIScrollView *)scrollView {
float pv = scrollView.contentOffset.x/scrollView.frame.size.width;
// floor取小于自己的整數(shù)
NSInteger lpv = floor(pv) + 1;
if (lpv == 0) { // 回到倒數(shù)第二頁(yè)
[scrollView setContentOffset:CGPointMake((_imagesArr.count-2)*scrollView.frame.size.width, 0) animated:NO];
}else if (lpv == _imagesArr.count) { // 回到第二頁(yè)
[scrollView setContentOffset:CGPointMake(scrollView.frame.size.width, 0) animated:NO];
}else {
}
}
第二種方式
使用三個(gè)view循環(huán)重用,默認(rèn)顯示中間位置桶至,即不管左滑還是右滑昼伴,滑動(dòng)后都在后臺(tái)回到中間view顯示。實(shí)現(xiàn)原理就是滑動(dòng)后镣屹,刷新三個(gè)view顯示數(shù)據(jù)圃郊,中間view顯示當(dāng)前view數(shù)據(jù),左右頁(yè)面對(duì)應(yīng)該數(shù)據(jù)的前后數(shù)據(jù)刷新頁(yè)面女蜈。
- (void) scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
[self reloadData];
}
- (void) reloadData {
CGPoint contentOffset = [_scrollView contentOffset];
CGFloat scrollViewWidth = [self scrollViewWidth];
NSLog(@"----%f---%f",scrollViewWidth, contentOffset.x);
if (contentOffset.x > scrollViewWidth) { //向左滑動(dòng)
NSLog(@"----向左滑動(dòng)");
_currentIndex = (_currentIndex + 1) % _dataCount;
} else if (contentOffset.x < scrollViewWidth) { //向右滑動(dòng)
NSLog(@"----向右滑動(dòng)");
_currentIndex = (_currentIndex - 1 + _dataCount) % _dataCount;
}else {
NSLog(@"-----未滑動(dòng)");
return;
}
[self setViewDataWithCurrentIndex:_currentIndex];
}
/// 刷新view上顯示的數(shù)據(jù)持舆,讓中間view顯示當(dāng)前view數(shù)據(jù)色瘩,然后滑動(dòng)scrollview到中間位置,每次滑動(dòng)都保持顯示中間位置
/// @param index 當(dāng)前顯示數(shù)據(jù)的下標(biāo)
- (void) setViewDataWithCurrentIndex:(NSInteger)index {
if (!self.dataSource || ![self.dataSource respondsToSelector:@selector(cycleView:setViewData:index:)]) {
return;
}
// 設(shè)置中間view數(shù)據(jù)
[self.dataSource cycleView:self setViewData:_centerView index:_currentIndex];
// 滑動(dòng)到中間控件
[_scrollView setContentOffset:CGPointMake([self scrollViewWidth], 0) animated:NO];
// 設(shè)置左右兩邊view數(shù)據(jù)
[self.dataSource cycleView:self setViewData:_leftView index:(_currentIndex - 1 + _dataCount)%_dataCount];
[self.dataSource cycleView:self setViewData:_rightView index:(_currentIndex + 1)%_dataCount];
}
注意:這種方案的判斷方法就是上面說(shuō)的問(wèn)題逸寓。
第三種方式
使用uicollectionview居兆,可以設(shè)置滑動(dòng)方向
layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
設(shè)置分頁(yè)
collectionView.pagingEnabled = YES;
滑動(dòng)判斷,這里要用到第一種方案組合實(shí)現(xiàn)竹伸,數(shù)據(jù)前后更增加一個(gè)泥栖。
- (void) scrollViewDidScroll:(UIScrollView *)scrollView {
float pv = scrollView.contentOffset.x/scrollView.frame.size.width;
NSInteger lpv = floor(pv) + 1;
if (lpv == 0) { // 回到倒數(shù)第二頁(yè)
[scrollView setContentOffset:CGPointMake((_imagesArr.count-2)*scrollView.frame.size.width, 0) animated:NO];
}else if (lpv == _imagesArr.count) { // 回到第二頁(yè)
[scrollView setContentOffset:CGPointMake(scrollView.frame.size.width, 0) animated:NO];
}else {
}
}
注意:這種方案的好處是可以使用系統(tǒng)uicollectionview的cell的重用機(jī)制,保證不會(huì)創(chuàng)建很多cell勋篓;簡(jiǎn)單聊倔,方便,快捷生巡。