滚动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; // 分页控制器当前显示的页数
}
效果如下:
相关文章推荐
- Codeforces Round #241 (Div. 2) AGuess a number!
- 多线程并发问题(二)
- Android 入门(1)
- iOS使用NSMutableAttributedString实现富文本
- 借力“搜索营销漏斗”SEM效果更上一层楼
- 求两个有序数组的中位数
- 异常处理,使用实体类
- Java集合总览
- Android_ConnectivityManager连接管理
- Python中的“_"
- 打印日历
- hdu 5402 一道神奇的模拟题
- 题目:最小路径和
- Android 使用外部db数据库文件
- Android动态设置actionBar
- Servlet 容器对URI的处理
- hdu 5398 GCD Tree 2015多校联合训练赛#9 LCT,动态生成树
- Struts(3)工作机制及分析
- 题目:最小子数组
- 2015/8/19/FramLayout/TableLayout/AbsoluteLayout