基本上每個(gè)APP都會(huì)有無限輪播廣告坎缭,在這里我通過UICollectionView來實(shí)現(xiàn)無線輪播
因?yàn)橥ㄟ^UICollectionView實(shí)現(xiàn)輪播缔御,一方面是因?yàn)閁ICollectionViewCell的復(fù)用犁罩,能更好的優(yōu)化內(nèi)存。
創(chuàng)建UICollectionView
- (void)setupUI{
UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
_collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(10, 5, kScreenWidth-20, kScrollViewH-10) collectionViewLayout:layout];
_collectionView.delegate = self;
_collectionView.dataSource = self;
[_collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"cell1"];
layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
layout.itemSize = CGSizeMake(kScreenWidth -20, kScrollViewH-10);
layout.minimumLineSpacing = 0;
_collectionView.pagingEnabled = YES;
_collectionView.showsHorizontalScrollIndicator = NO;
_collectionView.showsVerticalScrollIndicator = NO;
[self addSubview:_collectionView];
self.pageControl = [[UIPageControl alloc]init];
self.pageControl.frame = CGRectMake(0, kScrollViewH - 30, kScreenWidth, 30);
[self addSubview:self.pageControl];
NSTimer *timer = [NSTimer scheduledTimerWithTim
eInterval:2.0 target:self selector:@selector(timerMethed) userInfo:nil repeats:YES];
NSRunLoop *runLoop = [NSRunLoop currentRunLoop];
[runLoop addTimer:timer forMode:NSRunLoopCommonModes];
}
實(shí)現(xiàn)無線輪播輪播方式
#pragma mark - collection Delegate
-(UICollectionViewCell*)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
UICollectionViewCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:@"cell1" forIndexPath:indexPath];
cell.backgroundColor = numArray[indexPath.row%numArray.count];
return cell;
}
- (NSInteger)collectionView:(UICollectionView *)view numberOfItemsInSection:(NSInteger)section;
{
return numArray.count *10000;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
[self.delegate didSelectWithIndex:indexPath.row%numArray.count];
}
#pragma mark - ScrollViewDelegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
CGFloat offset_x = scrollView.contentOffset.x/(kScreenWidth - 20);
if (offset_x>numArray.count) {
NSIndexPath *indexPath = [NSIndexPath indexPathForItem:0 inSection:0];
[self.collectionView scrollToItemAtIndexPath:indexPath atScrollPosition:UICollectionViewScrollPositionNone animated:NO];
}else if(offset_x <0) {
NSIndexPath *indexPath = [NSIndexPath indexPathForItem:numArray.count inSection:0];
[self.collectionView scrollToItemAtIndexPath:indexPath atScrollPosition:UICollectionViewScrollPositionNone animated:NO];
}
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
CGFloat offset_x = scrollView.contentOffset.x/(kScreenWidth - 20);
if (offset_x>numArray.count-1) {
_pageControl.currentPage = (NSInteger)offset_x%numArray.count;
}else{
_pageControl.currentPage = offset_x;
}
}
我在numberOfItemsInSection*10000是為了更好往后滑動(dòng),
再說沒有用戶能需要滑到1W下左右的庇楞。捏卓。
為了實(shí)現(xiàn)外部點(diǎn)擊獲取當(dāng)前輪播圖的位置 這里添加了個(gè)代理极祸,用來外部實(shí)現(xiàn)點(diǎn)擊效果
@protocol ShufflingDelegate <NSObject>
@optional
//點(diǎn)擊某個(gè)輪播圖的指定的位置
- (void)didSelectWithIndex:(NSInteger)index;
@end
Demo地址:https://github.com/lanjiaoli/ShullfingScrollView.git
有不足的地方,大家可以提意見怠晴。