UICollectionView

2018-05-09  本文已影响6人  曾柏超
IMG_0001.PNG Screen Shot 2018-05-09 at 11.06.30 AM.png

ViewController

//
//  ViewController.m
//  CollectionView-LineLayout
//
//  Created by Kobe24 on 2018/1/2.
//  Copyright © 2018年 SYD. All rights reserved.
//

#import "ViewController.h"
#import "LineLayout.h"
#import "LineCollectionViewCell.h"

#define W [UIScreen mainScreen].bounds.size.width

@interface ViewController ()<UICollectionViewDataSource>

@property (nonatomic, strong) UICollectionView *lineCollectionView;
@property(nonatomic,strong)NSArray *data;
@end

@implementation ViewController

-(NSArray *)data{
    
    if (_data == nil) {
        _data = @[@"5/1",@"5/2",@"5/3",@"5/4",@"5/5",@"5/6",@"5/7",@"5/8",@"5/9",@"5/10"];
    }
    return _data;
}

- (UICollectionView *)lineCollectionView{
    if (_lineCollectionView == nil) {
        LineLayout *layout = [[LineLayout alloc] init];
//        layout.sectionInset = UIEdgeInsetsMake(75, W/2 - 150/2, 75, W/2 - 150/2);
//          _lineCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, W, 300) collectionViewLayout:layout];
        
        _lineCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 100, W, 70) collectionViewLayout:layout];
        _lineCollectionView.dataSource = self;
        _lineCollectionView.backgroundColor = [UIColor lightGrayColor];
        [_lineCollectionView registerClass:[LineCollectionViewCell class] forCellWithReuseIdentifier:@"cellID"];
    }
    return _lineCollectionView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self.view addSubview:self.lineCollectionView];
    
    
    
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return 10;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    LineCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cellID" forIndexPath:indexPath];
    
    NSLog(@"%@",self.data[indexPath.item]);
    cell.name = self.data[indexPath.item];
    cell.block = ^(NSString * str) {
        NSLog(@"%@", str);
    };
    return cell;
}



@end


LineCollectionViewCell

#import <UIKit/UIKit.h>

@interface LineCollectionViewCell : UICollectionViewCell

@property(nonatomic,strong) void (^block)(NSString *);

@property(nonatomic ,strong)NSString *name;
@property(nonatomic ,weak)UIButton *button;
@end


//
//  LineCollectionViewCell.m
//  CollectionView-LineLayout
//
//  Created by Kobe24 on 2018/1/2.
//  Copyright © 2018年 SYD. All rights reserved.
//

#import "LineCollectionViewCell.h"

@implementation LineCollectionViewCell

-(instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        self.backgroundColor = [UIColor redColor];
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.frame = CGRectMake(0, 0, 40, 30);
        [button setTitle:@"5/9" forState:UIControlStateNormal];
        [button setFont:[UIFont systemFontOfSize:12]];
        [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [button addTarget:self action:@selector(tap:) forControlEvents:UIControlEventTouchUpInside];
        
        _button = button;
        
        self.layer.cornerRadius = 10;
        self.layer.masksToBounds = YES;
        
        [self.contentView addSubview:button];
        
    }
    return self;
}

-(void)setName:(NSString *)name{
    
    [self.button setTitle:name forState:UIControlStateNormal];
}

-(void)tap:(UIButton *)btn{
    
    if (_block) {
        _block(btn.titleLabel.text);
    }

}

@end


LineLayout

#import <UIKit/UIKit.h>

@interface LineLayout : UICollectionViewFlowLayout

@end

//
//  LineLayout.m
//  CollectionView-LineLayout
//
//  Created by Kobe24 on 2018/1/2.
//  Copyright © 2018年 SYD. All rights reserved.
//

#import "LineLayout.h"
//
//#define ItemSize 150
//#define LineSpacing 50
#define ItemWidth 40
#define ItemHeight 30
#define LineSpacing 10
@implementation LineLayout

- (instancetype)init{
    if (self = [super init]) {
        self.itemSize = CGSizeMake(ItemWidth, ItemHeight);
        self.minimumLineSpacing = LineSpacing;
        self.collectionView.decelerationRate = UIScrollViewDecelerationRateFast;//速率
        self.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        
        self.sectionInset = UIEdgeInsetsMake(20, 20, 20,20);
        //水平方向
    }
    return self;
}

//返回滚动停止的点 自动对齐中心
//- (CGPoint)targetContentOffsetForProposedContentOffset:(CGPoint)proposedContentOffset withScrollingVelocity:(CGPoint)velocity{
//
//    CGFloat  offSetAdjustment = MAXFLOAT;
//
//    //预期停止水平中心点
//    CGFloat horizotalCenter = proposedContentOffset.x + self.collectionView.bounds.size.width / 2;
//
//    //预期滚动停止时的屏幕区域
//    CGRect targetRect = CGRectMake(proposedContentOffset.x, 0, self.collectionView.bounds.size.width, self.collectionView.bounds.size.height);
//
//    //找出最接近中心点的item
//    NSArray *array = [super layoutAttributesForElementsInRect:targetRect];
//    for (UICollectionViewLayoutAttributes * attributes in array) {
//        CGFloat currentCenterX = attributes.center.x;
//        if (ABS(currentCenterX - horizotalCenter) < ABS(offSetAdjustment)) {
//            offSetAdjustment = currentCenterX - horizotalCenter;
//        }
//    }
//    //
//    return CGPointMake(proposedContentOffset.x + offSetAdjustment, proposedContentOffset.y);
//}

//- (NSArray<UICollectionViewLayoutAttributes *> *)layoutAttributesForElementsInRect:(CGRect)rect{
//
//    NSArray *original = [super layoutAttributesForElementsInRect:rect];
//    NSArray *array = [[NSArray alloc] initWithArray:original copyItems:YES];
//
//    CGRect visibleRect;
//    visibleRect.origin = self.collectionView.contentOffset;
//    visibleRect.size = self.collectionView.bounds.size;
//
//    for (UICollectionViewLayoutAttributes * attributes in array) {
//        //判断相交
//        if (CGRectIntersectsRect(visibleRect, rect)) {
//            //当前视图中心点 距离item中心点距离
//       CGFloat  distance  =  CGRectGetMidX(self.collectionView.bounds) - attributes.center.x;
//            CGFloat  normalizedDistance = distance / 200;
//            if (ABS(distance) < 200) {
//                CGFloat zoom = 1 + 0.4 * (1 - ABS(normalizedDistance));
//                attributes.transform3D = CATransform3DMakeScale(zoom, zoom, 1);
//                attributes.zIndex = 1;
//            }
//        }
//    }
//
//    return array;
//}


- (BOOL)shouldInvalidateLayoutForBoundsChange:(CGRect)newBounds{
    return YES;
}

@end



上一篇下一篇

猜你喜欢

热点阅读