iOS开发之collectionView实现无限轮播视图


#import <UIKit/UIKit.h>

static NSString *ID = @"bannerCell";

@class BannerView;
@protocol BannerViewDelegate <NSObject>

@optional
- (void)bannerView:(BannerView *)bannerView didSelectedAtIndex:(NSInteger)nIndex;

@end

@interface BannerView : UIView

@property(nonatomic, strong)NSArray *imageArray;
@property(nonatomic, assign)CGFloat autoScrollTimeInterval;
@property(nonatomic, weak)id<BannerViewDelegate>  delegate;
+ (instancetype)bannerViewWithFrame:(CGRect)frame imageArray:(NSArray *)imageArray;

@end
</span>

.m文件实现

<span style="font-size:12px;">#import "BannerView.h"
#import "BannerViewCell.h"
#import <Masonry/Masonry.h>

@interface BannerView () <UICollectionViewDataSource, UICollectionViewDelegate>
@property(nonatomic, weak)UICollectionView *collectionView;
@property(nonatomic, weak)UICollectionViewFlowLayout *flowLayout;
@property(nonatomic, strong)NSTimer *timer;
@property(nonatomic, assign)NSInteger totalItemsCount;
@property(nonatomic, weak)UIPageControl *pageControl;
@end

@implementation BannerView
+ (instancetype)bannerViewWithFrame:(CGRect)frame imageArray:(NSArray *)imageArray
{
    BannerView *bannerView = [[self alloc]initWithFrame:frame];
    bannerView.imageArray = imageArray;
    return bannerView;
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor orangeColor];
        [self SetupSubviews];
    }
    
    return self;
}

- (void)SetupSubviews
{
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
    flowLayout.itemSize = self.frame.size;
    flowLayout.minimumLineSpacing = 0;
    flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    self.flowLayout = flowLayout;
    
    UICollectionView *collectionView = [[UICollectionView alloc]initWithFrame:self.frame
                                                     collectionViewLayout:flowLayout];
    
    collectionView.pagingEnabled = YES;
    collectionView.showsHorizontalScrollIndicator = NO;
    collectionView.showsVerticalScrollIndicator = NO;
    [collectionView registerClass:[BannerViewCell class] forCellWithReuseIdentifier:ID];
    collectionView.dataSource = self;
    collectionView.delegate = self;
    [self addSubview:collectionView];
    self.collectionView = collectionView;
    
    UIPageControl *pageControl = [[UIPageControl alloc] init];
    pageControl.currentPageIndicatorTintColor = [UIColor colorWithRed:85/255.0 green:132/255.0 blue:213/255.0 alpha:1.0];
    pageControl.pageIndicatorTintColor = [UIColor whiteColor];
    [self addSubview:pageControl];
    self.pageControl = pageControl;
}


- (void)setFrame:(CGRect)frame
{
    [super setFrame:frame];
    self.flowLayout.itemSize = self.frame.size;
}


- (void)setAutoScrollTimeInterval:(CGFloat)autoScrollTimeInterval
{
    _autoScrollTimeInterval = autoScrollTimeInterval;
    
    [_timer invalidate];
    _timer = nil;
    [self setupTimer];
}


- (void)layoutSubviews
{
    [super layoutSubviews];
    
    __weak __typeof(&*self)weakSelf = self;
    [self.pageControl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(weakSelf);
        make.bottom.mas_equalTo(weakSelf.mas_bottom);
        make.width.mas_equalTo(weakSelf.bounds.size.width/5.0);
    }];
    
    _collectionView.frame = self.bounds;
    if (_collectionView.contentOffset.x == 0) {
        [_collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:_totalItemsCount*0.5 inSection:0]
                            atScrollPosition:UICollectionViewScrollPositionNone animated:NO];
    }
}

- (void)setImageArray:(NSArray *)imageArray
{
    _imageArray = imageArray;
    self.pageControl.numberOfPages = imageArray.count;
    _totalItemsCount = imageArray.count*1000;
}

- (void)setupTimer
{
    NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:self.autoScrollTimeInterval target:self
                                                    selector:@selector(automaticScroll) userInfo:nil repeats:YES];
    _timer = timer;
    [[NSRunLoop mainRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];
}


- (void)automaticScroll
{
    int currentIndex = _collectionView.contentOffset.x / _flowLayout.itemSize.width;
    int targetIndex = currentIndex + 1;
    if (targetIndex == _totalItemsCount) {
        targetIndex = _totalItemsCount*0.5;
        [_collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:targetIndex inSection:0] atScrollPosition:UICollectionViewScrollPositionNone animated:NO];
    }
    [_collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:targetIndex inSection:0] atScrollPosition:UICollectionViewScrollPositionNone animated:YES];
}

#pragma mark -- UICollectionView DataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return _totalItemsCount;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    BannerViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:ID forIndexPath:indexPath];
    
    long itemIndex = indexPath.item % self.imageArray.count;
    cell.imageView.image = self.imageArray[itemIndex];
    
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    long nIndex = indexPath.item%self.imageArray.count;
    
    if ([self.delegate respondsToSelector:@selector(bannerView:didSelectedAtIndex:)]) {
        [self.delegate bannerView:self didSelectedAtIndex:nIndex];
    }
}

#pragma mark - UIScrollView Delegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    int currentIndex = _collectionView.contentOffset.x / _flowLayout.itemSize.width;
    NSInteger page = currentIndex%self.imageArray.count;
    self.pageControl.currentPage = page;
}

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    [_timer invalidate];
    _timer = nil;
}

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
    [self setupTimer];
}



@end
</span>

自定义cell:

<span style="font-size:12px;">#import <UIKit/UIKit.h>

@interface BannerViewCell : UICollectionViewCell
@property (weak, nonatomic) UIImageView *imageView;
@end</span>

<span style="font-size:12px;">#import "BannerViewCell.h"

@implementation BannerViewCell
- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        UIImageView *imageView = [[UIImageView alloc] init];
        [self.contentView addSubview:imageView];
        _imageView = imageView;
    }
    
    return self;
}


- (void)layoutSubviews
{
    [super layoutSubviews];
    
    _imageView.frame = self.contentView.frame;
}
@end</span>






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值