码迷,mamicode.com
首页 > 其他好文 > 详细

UICollectionView 小记

时间:2018-07-22 19:24:38      阅读:137      评论:0      收藏:0      [点我收藏+]

标签:highlight   poi   index   rac   before   elf   rom   direction   cga   

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 小记

标签:highlight   poi   index   rac   before   elf   rom   direction   cga   

原文地址:https://www.cnblogs.com/cnman/p/9350990.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!