1、自定义UICollectionViewLayout旋转效果

  之前有自定义UICollectionViewLayout(适用于多个section),本文是一个对cell进行CATransform3D操作的自定义!

2、相关代码

#import "CollectionFlowLayout.h"

@interface CollectionFlowLayout()
//存放每一个cell的属性
@property (nonatomic, retain) NSMutableArray *attributesArray; @end @implementation CollectionFlowLayout
/**
重写系统prepareLayout方法 (设置item的坐标等属性)
*/
- (void)prepareLayout{
[super prepareLayout];
_attributesArray = [[NSMutableArray alloc] init];
CGFloat inset = self.collectionView.bounds.size.width * (/64.0f);
inset = floor(inset);
self.scrollDirection = UICollectionViewScrollDirectionHorizontal;
CGFloat totalWidth = self.collectionView.frame.size.width;
self.collectionView.pagingEnabled = YES; NSUInteger itemCount = [self.collectionView numberOfItemsInSection:];
for (int i=; i<itemCount; i++) {
NSIndexPath *indexPath = [NSIndexPath indexPathForItem:i inSection:];
UICollectionViewLayoutAttributes *attributes = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:indexPath];
CGFloat baseOffsetForCurrentView = i* self.collectionView.bounds.size.width;
CGFloat currentOffset = self.collectionView.contentOffset.x;
//通过偏移量currentOffset 和baseOffsetForCurrentView 差值 得到collectionView宽度的倍数
//例如:当row为2时,是第三行,baseOffsetForCurrentView是: width*2 偏移量是 :width*2
// 滑动到第二行时 偏移量为:width,角度计算为 (width-width*2)/width 为-0.5
// 滑动到第四行时 偏移量为:width*3,角度计算为 (width*3-width*2)/width 为0.5
// 所以角度为 -0.5 —— 0.5变化,通过第二行和第四行的正负判断是左划还是右划确定axis值
attributes.transform3D = [self transformfromAngle:(currentOffset-baseOffsetForCurrentView)/totalWidth/ xAxis:(currentOffset-baseOffsetForCurrentView)>?YES:NO];
attributes.frame = CGRectMake(inset+(totalWidth)*i, , self.collectionView.bounds.size.width - ( *inset), self.collectionView.bounds.size.height * /);
[_attributesArray addObject:attributes];
} } /**
当collection view的bounds改变时,布局需要告诉collection view是否需要重新计算布局
*/
- (BOOL)shouldInvalidateLayoutForBoundsChange:(CGRect)newBounds {
return YES;
} /**
3D动画
*/
- (CATransform3D)transformfromAngle:(CGFloat )angle xAxis:(BOOL)axis
{
CATransform3D t = CATransform3DIdentity;
t.m34 = 1.0/-;
t = CATransform3DRotate(t,angle, axis?:-,, );
return t;
} /**
返回collectionView的界面显示大小
*/
- (CGSize)collectionViewContentSize{
return CGSizeMake(self.collectionView.frame.size.width*[self.collectionView numberOfItemsInSection:], self.collectionView.frame.size.height);
} /**
将所有的layoutAttributes重写布局
*/
- (nullable NSArray<__kindof UICollectionViewLayoutAttributes *> *)layoutAttributesForElementsInRect:(CGRect)rect{
return _attributesArray;
}
@end

最新文章

  1. 控制台游戏引擎CGE——贪吃蛇
  2. python 最大公约数
  3. 组合数学 + STL --- 利用STL生成全排列
  4. JavaScript及其异步实现续:Promise让一切更简单
  5. 动态规划(DP),模拟
  6. hdu-------1081To The Max
  7. PUA
  8. 关于java.lang.String理解中的一些难点
  9. checkbox在jquery版本1.9 以上用attr不可重复操作的问题【附解决方案】
  10. QLGame 2d Engine源码地址
  11. Struts中常用的几个技术
  12. 精通 Oracle+Python,第 7 部分:面向服务的 Python 架构
  13. 不可错过的手机APP常见8种界面导航样式
  14. linux (ubuntu) 下设置 tomcat 随系统自动启动
  15. Java &quot;==和equals区别&quot;
  16. mysql下的SELECT INTO语句
  17. (原创)我对未来的人类的发展,以及AI技术发展的一些思考。
  18. sql中查询同一列所有值出现的次数
  19. jdbc参数
  20. 【原创】源码角度分析Android的消息机制系列(二)——ThreadLocal的工作过程

热门文章

  1. nio读取文件,输出文件
  2. ES6 学习 -- Generator函数
  3. lds 文件说明
  4. 前端常用的库和实用技术之JavaScript高级技巧
  5. 自动化测试之sikuli调研
  6. 什么是 Hexo?
  7. ReentrantLock中的公平锁与非公平锁
  8. zabbix配置文件解析
  9. Java 虚拟机 - GC机制
  10. [JZOJ3234] 阴阳