您的位置:首页 > 其它

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

2015-08-19 19:38 381 查看

#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; // 分页控制器当前显示的页数
}

效果如下:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: