滚动CollectionView控制PageControl实现分页浏览的效果
2015-11-12 14:31
1496 查看
滚动CollectionView控制PageControl实现分页浏览的效果
原文章地址:http://blog.csdn.net/a_ss_a/article/details/47785013UIPageControlUICollectionViewIOSUICollectionView分页效果滚动CollectionView控制Pa
#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; // 分页控制器当前显示的页数
}
相关文章推荐
- 笔试大全(持续补充)
- RadioGroup里设置RadioButton为初始的都不选中状态及设置默认选中出现的问题及解决方法
- 使用afn 上传图片
- whireshark工具使用和tcp报文学习心得
- 旭说数据结构之线性表
- Quartz 入门
- iOS中UIPickerView详解
- UIView的层次结构--code
- org.http.HttpResponse cannot be resolved 错误
- 三菱PLCFX3U控制步进电机联动
- jQuery中添加自定义或函数方法
- Java读写XML文件
- JS实现回到顶部效果
- .emacs
- 配置java环境变量(linux mac windows7)
- 虚拟机保护技术浅谈
- 搭建中级注册码(网络验证码)系统教程
- 克鲁斯卡尔(Kruskal)算法求最小生成树
- ubuntu apache2 php 中文乱码
- C语言练习作业(三)