您的位置:首页 > 产品设计 > UI/UE

第04天实战技术(15):UICollectionView计算布局尺寸

2017-03-30 00:00 501 查看
#####一、UICollectionView计算布局尺寸

取绝对值
fabs


1.添加滚动动画进行缩放

效果:越靠近中心点,距离越小,缩放越大
思路:
0.获取当前显示区域 (self.collectionView.bounds)
1.求cell与中心点的距离 attr.centerX-offsetX-collectionW * 0.5
2.计算比例 (距离越小,缩放越大 所以需要取反)
>>>
// (attr.center.x - self.collectionView.contentOffset.x) 计算出来是 cell的center.x
// cell的center -collectionView的宽度一半 计算出来是 中心点的距离 取绝对值


code

// 可以一次性返回所有cell的尺寸,也可以每隔一个距离返回cell的尺寸
- (nullable NSArray<__kindof UICollectionViewLayoutAttributes *> *)layoutAttributesForElementsInRect:(CGRect)rect
{

// 设置cell的尺寸 ==> UICollectionViewLayoutAttributes =>

// 效果越靠近中心点,距离越小,缩放越大
// 求cell与中心点的距离 attr.centerX-offsetX-collectionW * 0.5

// 0.获取当前显示区域 (self.collectionView.bounds)
// 1.获取当前显示cell的布局
NSArray *attrs = [super layoutAttributesForElementsInRect:self.collectionView.bounds];

for (UICollectionViewLayoutAttributes *attr in attrs) {
// 进行缩放
// 2.计算中心点的距离 (取绝对值)
// (attr.center.x - self.collectionView.contentOffset.x) 计算出来是 cell的center.x
// cell的center -collectionView的宽度一半 计算出来是 中心点的距离 取绝对值
CGFloat delta = fabs((attr.center.x - self.collectionView.contentOffset.x) - self.collectionView.bounds.size.width * 0.5);
// 3.计算比例 (距离越小,缩放越大 所以需要取反)
CGFloat scale = 1 - delta / (self.collectionView.bounds.size.width * 0.5) * 0.25; // 1 - 1 * 0.25 = 0.75的缩放比例
attr.transform = CGAffineTransformMakeScale(scale, scale);
}
return attrs;
}


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息