美文网首页UI
iOS-自定义轮播图(UICollectionView)

iOS-自定义轮播图(UICollectionView)

作者: iOS阿能 | 来源:发表于2018-12-17 17:58 被阅读0次

核心部分仅仅需要timer和scrollview的代理

@interface FTHomeIndexHeaderView()<UIScrollViewDelegate,UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>

@property (weak, nonatomic) IBOutlet UICollectionView *collectionView;

@property (nonatomic,strong) NSTimer *timer;
@property (nonatomic,assign) int currentPage;

@end

@implementation FTHomeIndexHeaderView

- (void)awakeFromNib{
    [super awakeFromNib];
    [_collectionView registerNib:[UINib nibWithNibName:@"FTHomeHeaderIndexCell" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:@"FTHomeHeaderIndexCell"];
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    _dataArr = @[@1,@1,@1,@1,@1,@1];
    [self setupTimer];
}

- (void)setDataArr:(NSArray *)dataArr{
    _dataArr = dataArr;
    [self invalidateTimer];
    // 移動到開頭
    [_collectionView setContentOffset:CGPointMake(0, 0)];
    [_collectionView reloadData];
    _currentPage = 0;
    [self setupTimer];
}

#pragma mark -Timer

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

- (void)invalidateTimer{
    [self.timer invalidate];
}

- (void)autoNextPage{
    if (_dataArr.count < 1) {
        return; // 只有一個值,不需要輪播
    }
    // 新的目標頁數
    _currentPage = (_currentPage + 1) % _dataArr.count;
    NSLog(@"autoNextPage %d",_currentPage);
    [_collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:_currentPage inSection:0] atScrollPosition:UICollectionViewScrollPositionNone animated:YES];
}

//解决当父View释放时,当前视图因为被Timer强引用而不能释放的问题
- (void)willMoveToSuperview:(UIView *)newSuperview
{
    if (!newSuperview) {
        [self invalidateTimer];
    }
}

//解决当timer释放后 回调scrollViewDidScroll时访问野指针导致崩溃
- (void)dealloc {
    _collectionView.delegate = nil;
    _collectionView.dataSource = nil;
}


#pragma mark - UIScrollViewDelegate

- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset{
    _currentPage = (int)targetContentOffset ->x  / IPHONE_WIDTH;
}

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    [self invalidateTimer];
}

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

#pragma mark  - UICollectionViewDataSource & UICollectionViewDelegate & UICollectionViewDelegateFlowLayout

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return _dataArr.count;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    return [FTHomeHeaderIndexCell cellSize];
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    FTHomeHeaderIndexCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"FTHomeHeaderIndexCell" forIndexPath:indexPath];
    cell.lblnum.text = [NSString stringWithFormat:@"%ld",indexPath.row];
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    // 
}


@end

相关文章

网友评论

    本文标题:iOS-自定义轮播图(UICollectionView)

    本文链接:https://www.haomeiwen.com/subject/rzvckqtx.html