UICollectionView 小记
Posted cnman
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了UICollectionView 小记相关的知识,希望对你有一定的参考价值。
1.实现滚动缩放
自定义Layout继承
UICollectionViewFlowLayout
@implementation MyCollectionLayout - (void)prepareLayout{ [super prepareLayout]; self.scrollDirection = UICollectionViewScrollDirectionHorizontal; self.minimumLineSpacing = 1; } - (NSArray<UICollectionViewLayoutAttributes *> *)layoutAttributesForElementsInRect:(CGRect)rect{ NSArray *attrsArr = [super layoutAttributesForElementsInRect:rect]; NSMutableArray *newAttrsArr = [NSMutableArray arrayWithCapacity:attrsArr.count]; CGFloat centerX = self.collectionView.bounds.size.width/2; for (UICollectionViewLayoutAttributes* attr in attrsArr) { CGFloat distanceCenter = fabs(attr.center.x - centerX-self.collectionView.contentOffset.x); CGFloat scale = 1.2- distanceCenter/centerX; if (scale <0.8) { scale = 0.8; } attr.transform = CGAffineTransformMakeScale(scale, scale); [newAttrsArr addObject:attr]; self.itemWidth = attr.size.width; } return newAttrsArr; } - (BOOL)shouldInvalidateLayoutForBoundsChange:(CGRect)newBounds{ return true; } - (CGPoint)targetContentOffsetForProposedContentOffset:(CGPoint)proposedContentOffset withScrollingVelocity:(CGPoint)velocity { NSLog(@"before proposedContentOffset==%@",NSStringFromCGPoint(proposedContentOffset)); CGRect rect = CGRectMake(proposedContentOffset.x, 0, self.collectionView.bounds.size.width, self.collectionView.bounds.size.height); NSArray *attrs = [super layoutAttributesForElementsInRect:rect]; CGFloat collectionViewCenterX = self.collectionView.frame.size.width * 0.5; CGFloat minDistance = MAXFLOAT; //找出距离中点最近的item for (UICollectionViewLayoutAttributes *attr in attrs) { CGFloat distance = attr.center.x - proposedContentOffset.x - collectionViewCenterX; NSLog(@"distance ==%f",distance); if (fabs(distance) < fabs(minDistance)) { minDistance = distance; } } //位移一段保持居中 proposedContentOffset.x += minDistance; NSLog(@"after proposedContentOffset==%@",NSStringFromCGPoint(proposedContentOffset)); return proposedContentOffset; } @end
2.拖动排序item
这里给collectionview添加一个长按的手势进入拖动状态
- (void)move:(UILongPressGestureRecognizer*)reg{ if (reg.state == UIGestureRecognizerStateBegan) { [self.collectionView beginInteractiveMovementForItemAtIndexPath:[self.collectionView indexPathForItemAtPoint: [reg locationInView:self.collectionView]]]; cell = (WJLCollectionViewCell*)[self.collectionView cellForItemAtIndexPath:[self.collectionView indexPathForItemAtPoint: [reg locationInView:self.collectionView]]]; }else if (reg.state == UIGestureRecognizerStateChanged){ [self.collectionView updateInteractiveMovementTargetPosition:[reg locationInView:self.collectionView]]; }else if (reg.state == UIGestureRecognizerStateEnded) { [self.collectionView endInteractiveMovement]; }else{ [self.collectionView cancelInteractiveMovement]; } } - (void)collectionView:(UICollectionView *)collectionView moveItemAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath{ //处理移动后的逻辑 }
3.瀑布流
以上是关于UICollectionView 小记的主要内容,如果未能解决你的问题,请参考以下文章