1.targetContentOffsetForProposedContentOffset
//proposedContentOffset 内容视图的偏移量 velocity速度
//offsetAdjustment 应该调整的位置,设为最大为了循环取出最小
//horizontalCenter 内容的最左边到视图中心的位置
//targetRect 当前看到窗口中得边框
//layoutAttributes 视图中每一个cell的布局属性包含长宽等
//contentInset 内容视图在frame中得偏移上下左右等
//CGFloat cellW = (kDeviceWidth- CELL_MARGIN * (CELL_ROW + 1)) / CELL_ROW;
//UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
//
//layout.itemSize = CGSizeMake(cellW, cellW);// 定义cell的size
//layout.minimumInteritemSpacing = CELL_MARGIN;// 定义左右cell的最小间距
//layout.minimumLineSpacing = CELL_LINE_MARGIN;// 定义上下cell的最小间距
//layout.footerReferenceSize = CGSizeMake(kDeviceWidth, 50);// 定义headview的size
//layout.sectionInset = UIEdgeInsetsMake(5, 4, 5, 4);组内得cell显示区域内容的缩进
- (CGPoint)targetContentOffsetForProposedContentOffset:(CGPoint)proposedContentOffset withScrollingVelocity:(CGPoint)velocity
{
CGFloat offsetAdjustment = MAXFLOAT;
CGFloat horizontalCenter = proposedContentOffset.x + (CGRectGetWidth(self.collectionView.bounds) / 2.0);//
CGRect targetRect = CGRectMake(proposedContentOffset.x,0.0, self.collectionView.bounds.size.width,self.collectionView.bounds.size.height);
NSArray* array = [superlayoutAttributesForElementsInRect:targetRect];
for (UICollectionViewLayoutAttributes* layoutAttributesin array) {
CGFloat itemHorizontalCenter = layoutAttributes.center.x;
if (ABS(itemHorizontalCenter - horizontalCenter) <ABS(offsetAdjustment)) {
offsetAdjustment = itemHorizontalCenter - horizontalCenter;
}
}
return CGPointMake(proposedContentOffset.x + offsetAdjustment, proposedContentOffset.y);
}
2.layoutAttributesForElementsInRect
- (NSArray*)layoutAttributesForElementsInRect:(CGRect)rect
{
NSArray* array = [super layoutAttributesForElementsInRect:rect];
CGRect visibleRect;
visibleRect.origin = self.collectionView.contentOffset;
visibleRect.size = self.collectionView.bounds.size;
for (UICollectionViewLayoutAttributes* attributes in array) {
if (CGRectIntersectsRect(attributes.frame, rect)) {
CGFloat distance = CGRectGetMidX(visibleRect) - attributes.center.x;
CGFloat normalizedDistance = distance / ACTIVE_DISTANCE;
CGFloat zoom = 1 - ABS(0.1*normalizedDistance);
attributes.transform3D = CATransform3DMakeScale(zoom, zoom, 1.0);
attributes.zIndex = 1;
}
}
return array;
}
//rect这是任何布局类中最重要的方法了,同时可能也是最容易让人迷惑的方法。collection view调用这个方法并传递一个自身坐标系统中的矩形过去。这个矩形代表了这个视图的可见矩形区域(也就是它的bounds),你需要准备好处理传给你的任何矩形。
//visibleRect 内容视图可以看到的区域
//CGRectIntersectsRect ,矩形交叉区域//zIndex