现在使用卡片效果的app很多,之前公司让实现一种卡片效果,就写了一篇关于实现卡片的文章。文章最后附有demo

实现上我选择了使用UICollectionView ;用UICollectionViewFlowLayout来定制样式;下面看看具体实现

具体实现

1、创建UICollectionView

  1. - (void)createCollectionView {
  2. CGFloat pading = 0 * SCREEN_WIDTH/375;
  3. LHLeftCollocationView * layout = [[LHLeftCollocationView alloc]init];
  4. layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
  5. layout.minimumLineSpacing = pading;
  6. layout.minimumInteritemSpacing = pading;
  7. // UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
  8. // layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
  9. _collectionView3 = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 100, [UIScreen mainScreen].bounds.size.width, imageHeight * SCREEN_RATE) collectionViewLayout:layout];
  10. _collectionView3.tag = 33;
  11. _collectionView3.dataSource = self;
  12. _collectionView3.delegate = self;
  13. _collectionView3.bounces = NO;
  14. _collectionView3.alwaysBounceHorizontal = NO;
  15. _collectionView3.alwaysBounceVertical = NO;
  16. _collectionView3.backgroundColor = [UIColor grayColor];
  17. _collectionView3.showsHorizontalScrollIndicator = NO;
  18. _collectionView3.showsVerticalScrollIndicator = NO;
  19. [self.view addSubview:_collectionView3];
  20. [_collectionView3 registerClass:[CollectionViewCell class] forCellWithReuseIdentifier:collectionViewCell];
  21. }

2、实现具体代理方法 UICollectionViewDelegate,UICollectionViewDataSource

  1. - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
  2. return self.modelArray.count;
  3. }
  4. - (NSMutableArray *)modelArray {
  5. if (!_modelArray) {
  6. _modelArray = [NSMutableArray array];
  7. }
  8. return _modelArray;
  9. }
  10. - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
  11. CollModel *infoModel = self.modelArray[indexPath.row];
  12. NSLog(@"section:%ld --- row:%ld -----%@",indexPath.section,indexPath.row,infoModel.title);
  13. CollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:collectionViewCell forIndexPath:indexPath];
  14. cell.itemModel = infoModel;
  15. return cell;
  16. }
  17. // 返回每个item的大小
  18. - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
  19. CGFloat CWidth = 80 * SCREEN_RATE;
  20. CGFloat CHeight = 80 * SCREEN_RATE;
  21. return CGSizeMake(CWidth, CHeight);
  22. }
  23. #pragma mark - UICollectionViewDelegate点击事件
  24. - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
  25. CollModel *infoModel = self.modelArray[indexPath.row];
  26. NSLog(@"infoModelArray----%@",infoModel.title);

3、自定义UICollectionViewFlowLayout

LHLeftCollocationView.m 实现

  1. #import "LHLeftCollocationView.h"
  2. @implementation LHLeftCollocationView
  3. - (CGPoint)targetContentOffsetForProposedContentOffset:(CGPoint)proposedContentOffset withScrollingVelocity:(CGPoint)velocity {
  4. CGRect targectRect = CGRectMake(proposedContentOffset.x, 0.0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height);
  5. NSArray * attriArray = [super layoutAttributesForElementsInRect:targectRect];
  6. CGFloat horizontalCenterX = proposedContentOffset.x + ([UIScreen mainScreen].bounds.size.width);
  7. CGFloat offsetAdjustment = CGFLOAT_MAX;
  8. for (UICollectionViewLayoutAttributes * layoutAttributes in attriArray) {
  9. CGFloat itemHorizontalCenterX = layoutAttributes.center.x;
  10. if (fabs(itemHorizontalCenterX-horizontalCenterX) < fabs(offsetAdjustment)) {
  11. offsetAdjustment = itemHorizontalCenterX - horizontalCenterX;
  12. }
  13. }
  14. return CGPointMake(proposedContentOffset.x , proposedContentOffset.y);
  15. }
  16. CGFloat ActiveDistance = 400; //垂直缩放除以系数
  17. CGFloat ScaleFactor = 0.50; //缩放系数 越大缩放越大
  18. - (NSArray<UICollectionViewLayoutAttributes *> *)layoutAttributesForElementsInRect:(CGRect)rect {
  19. NSArray * array = [super layoutAttributesForElementsInRect:rect];
  20. CGRect visibleRect = CGRectZero;
  21. visibleRect.origin = self.collectionView.contentOffset;
  22. visibleRect.size = self.collectionView.bounds.size;
  23. for (UICollectionViewLayoutAttributes *attributes in array) {
  24. CGFloat distance = CGRectGetMidX(visibleRect) - attributes.center.x;
  25. CGFloat normalizedDistance = fabs(distance / ActiveDistance);
  26. CGFloat zoom = 1 - ScaleFactor * normalizedDistance;
  27. NSLog(@"zoom----%f",zoom);
  28. attributes.transform3D = CATransform3DMakeScale(1.0, zoom, 1.0);
  29. //底部显示效果
  30. attributes.frame = CGRectMake(attributes.frame.origin.x, attributes.frame.origin.y + zoom, attributes.size.width, attributes.size.height);
  31. //居中显示效果
  32. // CGFloat scrollDirectionItemHeight = self.itemSize.height;
  33. // CGFloat sideItemFixedOffset = 0;
  34. // sideItemFixedOffset = (scrollDirectionItemHeight - scrollDirectionItemHeight * 0.7) / 2;
  35. // attributes.center = CGPointMake(attributes.center.x, attributes.center.y + zoom);
  36. }
  37. return array;
  38. }
  39. ////设置放大动画
  40. //-(NSArray<UICollectionViewLayoutAttributes *> *)layoutAttributesForElementsInRect:(CGRect)rect
  41. //{
  42. // NSArray *arr = [self getCopyOfAttributes:[super layoutAttributesForElementsInRect:rect]];
  43. // //屏幕中线
  44. // CGFloat centerX = self.collectionView.contentOffset.x + self.collectionView.bounds.size.width/2.0f;
  45. // //刷新cell缩放
  46. // for (UICollectionViewLayoutAttributes *attributes in arr) {
  47. // CGFloat distance = fabs(attributes.center.x - centerX);
  48. // //移动的距离和屏幕宽度的的比例
  49. // CGFloat apartScale = distance/self.collectionView.bounds.size.width;
  50. // //把卡片移动范围固定到 -π/4到 +π/4这一个范围内
  51. // CGFloat scale = fabs(cos(apartScale * M_PI/4));
  52. // //设置cell的缩放 按照余弦函数曲线 越居中越趋近于1
  53. // attributes.transform = CGAffineTransformMakeScale(1.0, scale);
  54. // }
  55. // return arr;
  56. //}
  57. //防止报错 先复制attributes
  58. - (NSArray *)getCopyOfAttributes:(NSArray *)attributes
  59. {
  60. NSMutableArray *copyArr = [NSMutableArray new];
  61. for (UICollectionViewLayoutAttributes *attribute in attributes) {
  62. [copyArr addObject:[attribute copy]];
  63. }
  64. return copyArr;
  65. }
  66. - (BOOL)shouldInvalidateLayoutForBoundsChange:(CGRect)newBounds {
  67. return true;
  68. }
  69. @end

4、自定义cell 和model

model

  1. #import <Foundation/Foundation.h>
  2. @interface CollModel : NSObject
  3. @property (nonatomic,strong)NSString *imgUrl;
  4. @property (nonatomic,strong)NSString *title;
  5. @property (nonatomic,strong)NSString *url;
  6. @end

cell 自定义

  1. #import <UIKit/UIKit.h>
  2. #import "CollModel.h"
  3. @interface CollectionViewCell : UICollectionViewCell
  4. @property (nonatomic, strong) CollModel * itemModel;
  5. @end
  6. #import "CollectionViewCell.h"
  7. #define SCREEN_RATE ([UIScreen mainScreen].bounds.size.width/375.0)
  8. @interface CollectionViewCell()
  9. /**
  10. * 存放所有下载操作的队列
  11. */
  12. @property (nonatomic, strong) UIImageView *itemIcon;
  13. @property (nonatomic, strong) UILabel *itemLabel;
  14. @property (nonatomic, strong) UILabel *priceLabel;
  15. @end
  16. @implementation CollectionViewCell
  17. @synthesize itemModel = _itemModel;
  18. - (instancetype)initWithFrame:(CGRect)frame
  19. {
  20. if (self = [super initWithFrame:frame]) {
  21. self.contentView.backgroundColor = [UIColor clearColor];
  22. [self initView];
  23. }
  24. return self;
  25. }
  26. - (void)initView {
  27. _itemIcon = [[UIImageView alloc] init];
  28. [self.contentView addSubview:_itemIcon];
  29. _itemIcon.backgroundColor = [UIColor clearColor];
  30. // CGFloat iconWidth = ([UIScreen mainScreen].bounds.size.width / 5.0) * SCREEN_RATE;
  31. _itemIcon.frame = CGRectMake(0, 0, self.contentView.frame.size.width, self.contentView.frame.size.height);
  32. _itemIcon.center = self.contentView.center;
  33. }
  34. - (CollModel *)itemModel
  35. {
  36. return _itemModel;
  37. }
  38. - (void)setItemModel:(CollModel *)itemModel
  39. {
  40. if (!itemModel) {
  41. return;
  42. }
  43. _itemModel = itemModel;
  44. [self setCellWithModel:_itemModel];
  45. }
  46. - (void)setCellWithModel:(CollModel *)itemModel
  47. {
  48. [[NSOperationQueue mainQueue] addOperationWithBlock:^{
  49. _itemIcon.image = [UIImage imageNamed:itemModel.url];
  50. }];
  51. }
  52. @end

运行效果
https://zsrimg.ikafan.com/file_images/article/202004/2020421151158504.gif

更多相关文章

  1. Android实现手势滑动(左滑和右滑)
  2. Android利用android:indeterminateDrawable来实现ProgressBar三
  3. linux公社的大了免费在线android资料
  4. Android(安卓)支持多屏幕机制
  5. TextView 加链接所有方法
  6. Android(安卓)使用 MPAndroidChart 实现折线图
  7. Android(安卓)简单记事本
  8. Android在listview添加checkbox实现单选多选操作问题
  9. 利用模糊实现视觉3D效果实例讲解

随机推荐

  1. android TextView属性详解
  2. Android控件visible,invisible和gone
  3. Android ListView CheckBox点击事件
  4. Android月历控件(DatePicker)和时间控件(Tim
  5. Android - 文件读写操作 总结
  6. Android
  7. Android在开机时自动启动一个应用程序
  8. android 分隔线
  9. EditText小总结
  10. 【Android】Android 代码判断是否获取ROO