美文网首页
iOS--CustomFlowLayout

iOS--CustomFlowLayout

作者: STONEsh | 来源:发表于2016-02-28 15:37 被阅读130次

    ViewController.m#

    //
    //  ViewController.m
    //  CustomFlowLayout
    
    //
    
    #import "ViewController.h"
    #import "TestCollectionViewCell.h"
    #import "CustomFlowLayout.h"
    @interface ViewController ()<UICollectionViewDataSource,UICollectionViewDelegate>
    
    @end
    
    @implementation ViewController
    
    NSString *identifier = @"cell";
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        //创建布局对象
        CustomFlowLayout *flowLayout = [[CustomFlowLayout alloc] init];
        
        UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 20, 414, 200) collectionViewLayout:flowLayout];
        collectionView.delegate = self;
        collectionView.dataSource = self;
        [collectionView registerClass:[TestCollectionViewCell class] forCellWithReuseIdentifier:identifier];
        [self.view addSubview:collectionView];
        
        
        
    }
    
    
    
    
    - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
        return 20;
    }
    
    
    - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
        TestCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
        
        cell.imgView.image = [UIImage imageNamed:@"10.png" ];
        return cell;
    }
    
    - (void)didReceiveMemoryWarning {
        [super didReceiveMemoryWarning];
        // Dispose of any resources that can be recreated.
    }
    
    @end
    
    

    TestCollectionViewCell.h#

    //
    //  TestCollectionViewCell.h
    //  CustomFlowLayout
    //
    
    //
    
    #import <UIKit/UIKit.h>
    
    @interface TestCollectionViewCell : UICollectionViewCell
    
    @property(nonatomic,strong)UIImageView *imgView;
    
    @end
    
    

    TestCollectionViewCell.m#

    //
    //  TestCollectionViewCell.m
    //  UI_21_CustomFlowLayout
    //
    
    //
    
    #import "TestCollectionViewCell.h"
    
    @implementation TestCollectionViewCell
    
    -(instancetype)initWithFrame:(CGRect)frame{
        if (self = [super initWithFrame:frame]) {
            
            self.imgView = [[UIImageView alloc] initWithFrame:self.contentView.bounds];
            
            [self.contentView addSubview:_imgView];
        }
        
        return self;
    }
    
    
    @end
    
    

    CustomFlowLayout.m#

    //
    //  CustomFlowLayout.m
    //  CustomFlowLayout
    .
    //
    
    #import "CustomFlowLayout.h"
    
    #define kItem  100
    
    @implementation CustomFlowLayout
    
    
    //做好一些准备工作,初始化
    //准备布局
    -(void)prepareLayout{
        
        //设置大小
        self.itemSize = CGSizeMake(kItem, kItem);
        //设置滚动方向
        self.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        //最小行间距
        self.minimumLineSpacing = 50;
        
        
    }
    
    //是否时刻改变并且重新布局
    -(BOOL)shouldInvalidateLayoutForBoundsChange:(CGRect)newBounds{
        return YES;
    }
    
    
    //数组里放置的是每一个item设置的属性(例如: frame等等)
    -(NSArray<UICollectionViewLayoutAttributes *> *)layoutAttributesForElementsInRect:(CGRect)rect{
        //从父类里获取item放置属性的数组
        NSArray *array = [super layoutAttributesForElementsInRect:rect];
        //获取屏幕中心点的x坐标
        CGFloat centerX = self.collectionView.frame.size.width/2 + self.collectionView.contentOffset.x;
        //遍历
        for (UICollectionViewLayoutAttributes *attrs in array) {
            
            //item的中心点
            CGFloat itemCenterX = attrs.center.x;
            //缩放比例
            CGFloat scale = 1 + 0.5*(1 - ABS(itemCenterX - centerX))/200;
            //
            attrs.transform3D = CATransform3DMakeScale(scale, scale, 1);
        }
    
        return array;
    }
    
    @end
    
    

    相关文章

      网友评论

          本文标题:iOS--CustomFlowLayout

          本文链接:https://www.haomeiwen.com/subject/eyijkttx.html