自定义UICollectionViewLayout(二) ----StackLayout&CircleLayout
2016-04-08 15:46
267 查看
注意: 因为是继承于UICollectionViewLayout, 所以得实现一下
这个方法
StackLayout:
CircleLayout:
layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath
这个方法
StackLayout:
@implementation HMStackLayout /** 只要显示的边界发生改变就重新布局: * 内部会重新调用layoutAttributesForElementsInRect方法获得所有的cell的布局属性 */ - (BOOL)shouldInvalidateLayoutForBoundsChange:(CGRect)newBounds { return YES; } - (CGSize)collectionViewContentSize { return CGSizeMake(500, 500); } - (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath { NSArray *angles = @[@0, @(-0.2), @(-0.5), @(0.2), @(0.5)]; UICollectionViewLayoutAttributes *attrs = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:indexPath]; attrs.size = CGSizeMake(100, 100); attrs.center = CGPointMake(self.collectionView.frame.size.width * 0.5, self.collectionView.frame.size.height * 0.5); if (indexPath.item>=5) { attrs.hidden = YES; } else { attrs.transform = CGAffineTransformMakeRotation([angles[indexPath.item] floatValue]); // zIndex越大,就越在上面 attrs.zIndex = [self.collectionView numberOfItemsInSection:indexPath.section] - indexPath.item; } return attrs; } - (NSArray<UICollectionViewLayoutAttributes *> *)layoutAttributesForElementsInRect:(CGRect)rect { NSMutableArray *array = [NSMutableArray array]; NSInteger count = [self.collectionView numberOfItemsInSection:0]; for (int i = 0; i<count; i++) { UICollectionViewLayoutAttributes *attrs = [self layoutAttributesForItemAtIndexPath:[NSIndexPath indexPathForItem:i inSection:0]]; [array addObject:attrs]; } return array; } @end
CircleLayout:
@implementation HMCircleLayout - (BOOL)shouldInvalidateLayoutForBoundsChange:(CGRect)newBounds { return YES; } - (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath { UICollectionViewLayoutAttributes *attrs = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:indexPath]; attrs.size = CGSizeMake(100, 100); // 圆的半径 CGFloat circleRadius = 150; CGPoint circleCenter = CGPointMake(self.collectionView.frame.size.width * 0.5, self.collectionView.frame.size.height * 0.5); // 每个item之间的角度 CGFloat angleDelta = M_PI * 2 / [self.collectionView numberOfItemsInSection:indexPath.section]; // 计算当前item的角度 CGFloat angle = indexPath.item * angleDelta; attrs.center = CGPointMake(circleCenter.x + circleRadius * cosf(angle), circleCenter.y - circleRadius * sinf(angle)); return attrs; } - (NSArray<UICollectionViewLayoutAttributes *> *)layoutAttributesForElementsInRect:(CGRect)rect { NSMutableArray *array = [NSMutableArray array]; NSInteger count = [self.collectionView numberOfItemsInSection:0]; for (int i = 0; i<count; i++) { UICollectionViewLayoutAttributes *attrs = [self layoutAttributesForItemAtIndexPath:[NSIndexPath indexPathForItem:i inSection:0]]; [array addObject:attrs]; } return array; } @end
相关文章推荐
- 对Django框架架构和Request/Response处理流程的分析 | 疯狂的蚂蚁
- UIImageView+WebCache.h
- UI Thread
- UICoreAnimation(5):CATransition&amp;…
- UI单例类的设计,GCD的图片加载
- UIGCD的用法,组任务
- UI触摸手势,事件的分发传递
- UI: didReceiveMemoryWarning…
- Ui:响应者链,手势,摇动事件
- UI:音频播放,桥接,相片和照片处…
- UICoreAnimation(2):Calayer…
- SwipeRefreshLayout,设置setRefreshing(true)没效果的解决方案
- UI进栈出栈原理
- UICoreAnimation基础
- UICoreAnimation(3):核心动…
- UICoreAnimation(4):核心动…
- UICoreAnimation(4):核心动…
- UI多线程(1)并行和并发的…
- UIAlertController的使用方…
- UItransframe属性(形变)