滚动CollectionView控制PageControl实现分页浏览的效果

 

滚动CollectionView控制PageControl实现分页浏览的效果


原文章地址:http://blog.csdn.net/a_ss_a/article/details/47785013


#pragma mark  -- 主要实现在于自定义UICollectionViewFlowLayout

#pragma mark - SSGiftCollectionViewFlowLayout.h文件

#import <UIKit/UIKit.h>

@protocol CustomViewFlowLayoutDelegate <UICollectionViewDelegateFlowLayout>

- (void)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout cellCenteredAtIndexPath:(NSIndexPath *)indexPath page:(int)page;

@end

@interface SSGiftCollectionViewFlowLayout : UICollectionViewFlowLayout

@property (nonatomic, weak) id<CustomViewFlowLayoutDelegate> delegate;

@end

#pragma mark - SSGiftCollectionViewFlowLayout.m文件

@implementation SSGiftCollectionViewFlowLayout

- (void)prepareLayout{

    [super prepareLayout];

}

- (id)init {

    if (self = [super init]) {

        self.scrollDirection = UICollectionViewScrollDirectionHorizontal;

        self.minimumInteritemSpacing = 0.0f;

        self.sectionInset = UIEdgeInsetsZero;

        self.itemSize = CGSizeMake(100.f ,100.f);

        self.minimumLineSpacing = 0;

    }

    return self;

}


- (BOOL)shouldInvalidateLayoutForBoundsChange:(CGRect)newBounds {

    return YES;

}


- (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect {

    NSArray *attributes = [super layoutAttributesForElementsInRect:rect];

    CGRect visibleRect;

    visibleRect.origin = self.collectionView.contentOffset;

    visibleRect.size = self.collectionView.bounds.size;

    for (UICollectionViewLayoutAttributes *attribute in attributes) {

        if (CGRectIntersectsRect(attribute.frame, rect)) {

            if (visibleRect.origin.x == 0) {

                [self.delegate collectionView:self.collectionView layout:self cellCenteredAtIndexPath:attribute.indexPath page:0];

            }else{

                // 除法取整 取余数

                div_t x = div(visibleRect.origin.x,visibleRect.size.width);

                if (x.quot > 0 && x.rem > 0) {

                    [self.delegate collectionView:self.collectionView layout:self cellCenteredAtIndexPath:attribute.indexPath page:x.quot + 1];

                }

                if (x.quot > 0 && x.rem == 0) {

                    [self.delegate collectionView:self.collectionView layout:self cellCenteredAtIndexPath:attribute.indexPath page:x.quot];

                }

            }

        }

    }

    return attributes;

}


#pragma mark - CollectionView创建的主要代码

- (UICollectionView *)aCollectionView{

    if (_aCollectionView != nil) {

        return _aCollectionView;

    }

    SSGiftCollectionViewFlowLayout *viewFlowLayout = [[SSGiftCollectionViewFlowLayout alloc] init];

    viewFlowLayout.delegate = self;

    _aCollectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:viewFlowLayout];

    _aCollectionView.showsHorizontalScrollIndicator = FALSE; // 去掉滚动条

    _aCollectionView.alwaysBounceHorizontal = YES;

    _aCollectionView.pagingEnabled = YES;

    _aCollectionView.scrollEnabled = YES;

    _aCollectionView.delegate = self;

    _aCollectionView.dataSource = self;

    [_aCollectionView registerClass:[CustomCollectionViewCell class] forCellWithReuseIdentifier:identifier];

       return _aCollectionView;

}

#pragma mark - 实现CustomViewFlowLayoutDelegate

- (void)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout cellCenteredAtIndexPath:(NSIndexPath *)indexPath page:(int)page{

    self.pageControl.currentPage = page; // 分页控制器当前显示的页数

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值