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.瀑布流

猜你喜欢

转载自www.cnblogs.com/cnman/p/9350990.html