iosbanner_IOS--常用Banner循环滚动实现

实现步骤:

1.先整合数据源

//循环滚动,增加尾部和首部的数据源用以实现循环的视觉错觉

//如原@[a,b,c],整合成@[c,a,b,c,a]

self.lastX = 0;

NSMutableArray *tmpArray = [NSMutableArray arrayWithArray:URLArray];

[tmpArray addObject:[URLArray firstObject]];

[tmpArray insertObject:[URLArray lastObject] atIndex:0];

self.urlArray = [NSArray arrayWithArray:tmpArray];

[self.collectionView reloadData];

//collectionView刷新数据后再显示第一个数据页面

[self.collectionView performBatchUpdates:^{

[self.collectionView reloadData];

} completion:^(BOOL finished) {

[self.collectionView selectItemAtIndexPath:[NSIndexPath indexPathForItem:1 inSection:0] animated:NO scrollPosition:UICollectionViewScrollPositionLeft];

//开启滚动计时器

[self startTimer];

}];

2.计时器的设计

//开启计时器

- (void)startTimer {

[self stopTimer];

self.timer = [MSWeakTimer scheduledTimerWithTimeInterval:3 target:self selector:@selector(scrollToNextPage) userInfo:nil repeats:YES dispatchQueue:dispatch_get_main_queue()];

}

//collectionView滚动至下一页

- (void)scrollToNextPage {

NSIndexPath *indexpath = [self.collectionView indexPathForItemAtPoint:self.collectionView.contentOffset];

NSIndexPath *incrementPath = nil;

if (indexpath.item == [self.collectionView numberOfItemsInSection:0] - 1) {

//滚动至尾部

incrementPath = [NSIndexPath indexPathForItem:0 inSection:0];

} else if ([self.collectionView numberOfItemsInSection:0] == 0) {

//滚动至首部

incrementPath = [NSIndexPath indexPathForItem:0 inSection:0];

} else {

//中间部分滚动

incrementPath = [NSIndexPath indexPathForItem:indexpath.item + 1 inSection:0];

}

[self.collectionView scrollToItemAtIndexPath:incrementPath atScrollPosition:UICollectionViewScrollPositionRight animated:YES];

}

3.循环滚动实现

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{

//banner无限循环快速滑动

CGFloat currX = scrollView.contentOffset.x;

CGFloat pageX = currX - scrollView.frame.size.width;

NSInteger currCount = self.urlArray.count;

if (currCount > 1) {

if ((currX - _lastX) >= 0) {

//Banner右边区域的滑动

if(currX > (currCount-2)*scrollView.frame.size.width){

_lastX = 0;

[scrollView setContentOffset:CGPointMake(currX-(currCount-2)*scrollView.frame.size.width, 0)];

}

//pageVC滑动

if (currX < scrollView.frame.size.width) {

pageX = (currCount-2)*scrollView.frame.size.width;

}

}else{

//Banner左边区域的滑动

if(currX > (currCount-2)*scrollView.frame.size.width){

_lastX = 0;

[scrollView setContentOffset:CGPointMake(currX-(currCount-2)*scrollView.frame.size.width, 0)];

}else if (currX < scrollView.frame.size.width) {

_lastX = currCount*scrollView.frame.size.width;

[scrollView setContentOffset:CGPointMake(currX+(currCount-2)*scrollView.frame.size.width, 0)];

}

//pageVC滑动

if (currX < scrollView.frame.size.width) {

pageX = (currCount-2)*scrollView.frame.size.width;

}else if (currX > (currCount-2)*scrollView.frame.size.width){

pageX = 0;

}

}

int currPage = (int)(pageX/scrollView.frame.size.width);

self.pageControl.currentPage = currPage;

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值