UICollectionViewCell水平缩放,中间大两边小效果的实现

//

//  CollectionViewFlowLayout.h

//  HorizontalScale

//

//  Created by zmx on 16/3/15.

//  Copyright © 2016 zmx. All rights reserved.

//


#import <UIKit/UIKit.h>


@interface CollectionViewFlowLayout : UICollectionViewFlowLayout


@end



//

//  CollectionViewFlowLayout.m

//  HorizontalScale

//

//  Created by zmx on 16/3/15.

//  Copyright © 2016 zmx. All rights reserved.

//


#import "CollectionViewFlowLayout.h"


@implementation CollectionViewFlowLayout


- (NSArray<UICollectionViewLayoutAttributes *> *)layoutAttributesForElementsInRect:(CGRect)rect {

    NSArray *attrs = [self deepCopyWithArray:[super layoutAttributesForElementsInRect:rect]];

    CGFloat contentOffsetX = self.collectionView.contentOffset.x;

    CGFloat collectionViewCenterX = self.collectionView.frame.size.width * 0.5;

    for (UICollectionViewLayoutAttributes *attr in attrs) {

        CGFloat scale = 1 - fabs(attr.center.x - contentOffsetX - collectionViewCenterX) / self.collectionView.bounds.size.width;

        attr.transform = CGAffineTransformMakeScale(scale, scale);

    }

    return attrs;

}


- (BOOL)shouldInvalidateLayoutForBoundsChange:(CGRect)newBounds {

    return YES;

}


//  每次都有图片居中


- (CGPoint)targetContentOffsetForProposedContentOffset:(CGPoint)proposedContentOffset withScrollingVelocity:(CGPoint)velocity {

    CGRect rect = CGRectMake(proposedContentOffset.x, 0, self.collectionView.bounds.size.width, self.collectionView.bounds.size.height);

    NSArray *attrs = [super layoutAttributesForElementsInRect:rect];

    CGFloat contentOffsetX = self.collectionView.contentOffset.x;

    CGFloat collectionViewCenterX = self.collectionView.frame.size.width * 0.5;

    CGFloat minDistance = MAXFLOAT;

    for (UICollectionViewLayoutAttributes *attr in attrs) {

        CGFloat distance = attr.center.x - contentOffsetX - collectionViewCenterX;

        if (fabs(distance) < fabs(minDistance)) {

            minDistance = distance;

        }

    }

    proposedContentOffset.x += minDistance;

    return proposedContentOffset;

}


//  UICollectionViewFlowLayout has cached frame mismatch for index path这个警告来源主要是在使用layoutAttributesForElementsInRect:方法返回的数组时,没有使用该数组的拷贝对象,而是直接使用了该数组。解决办法对该数组进行拷贝,并且是深拷贝。


- (NSArray *)deepCopyWithArray:(NSArray *)arr {

    NSMutableArray *arrM = [NSMutableArray array];

    for (UICollectionViewLayoutAttributes *attr in arr) {

        [arrM addObject:[attr copy]];

    }

    return arrM;

}


@end



//

//  ViewController.m

//  HorizontalScale

//

//  Created by zmx on 16/3/15.

//  Copyright © 2016 zmx. All rights reserved.

//


#import "ViewController.h"

#import "CollectionViewFlowLayout.h"

#import "Cell.h"


static NSString *identifier = @"c";


@interface ViewController () <UICollectionViewDataSource, UICollectionViewDelegate>


@property (weak, nonatomic) IBOutlet UICollectionView *collectionView;


@property (weak, nonatomic) IBOutlet CollectionViewFlowLayout *layout;


@end


@implementation ViewController


- (void)viewDidLoad {

    [super viewDidLoad];

    // Do any additional setup after loading the view, typically from a nib.

    [self.collectionView registerNib:[UINib nibWithNibName:@"Cell" bundle:nil] forCellWithReuseIdentifier:identifier];

}


- (void)viewDidLayoutSubviews {

    [super viewDidLayoutSubviews];

    self.layout.itemSize = CGSizeMake(100, 100);

}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {

    return 20;

}


- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {

    Cell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];

    return cell;

}


@end

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值