滚动CollectionView控制PageControl实现分页浏览的效果
来源:互联网 发布:房租占收入比例 知乎 编辑:程序博客网 时间:2024/04/27 05:50
#pragma mark -- 主要实现在于自定义UICollectionViewFlowLayout
#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
@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;
}
- (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;
}
- (void)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout cellCenteredAtIndexPath:(NSIndexPath *)indexPath page:(int)page{
self.pageControl.currentPage = page; // 分页控制器当前显示的页数
}
效果如下:
- 滚动CollectionView控制PageControl实现分页浏览的效果
- 滚动CollectionView控制PageControl实现分页浏览的效果
- jQuery实现“分页”的效果,控制div分页显示
- Javascript的图片滚动浏览效果代码
- collectionView的反弹效果
- cxGrid类似pagecontrol的效果
- PageControl页显示的控制
- ios用collectionView实现无限滚动的简单日历
- scrollView分页和pageControl,广告栏实现
- 分页效果的实现
- 分页效果的实现
- (九)UIScrollView和PageControl的分页
- ios collectionview 的转场效果
- 分页控制 (是pagecontrol不是pagecontroller)和手势操作
- ListView,实现滚动分页效果。实现异步加载URL生成的图片。使得屏幕不卡
- JS控制图片滚动的效果.txt
- JS控制图片滚动的效果
- JS控制图片滚动的效果
- 题目:二叉树的前序遍历
- ListView 滑动时数据重复错位解决
- Android第三讲——五大布局(UI)
- 《UNIX环境高级编程》笔记--线程的取消选项
- 题目:二叉树的后序遍历
- 滚动CollectionView控制PageControl实现分页浏览的效果
- 黑马程序员—IO流
- 在delphi中Char,PChar,PByte,Byte,String的相互转换示例
- 题目:二叉树的最大深度
- 析构函数抛出异常
- Delphi流操作技巧
- HDU-1875 畅通工程再续(最小生成树[Prim])
- 题目:二叉树的最小深度
- 初步篇7分支结构程序体验项目1求两数正差值