ViewController.m

  1. //
  2. // ViewController.m
  3. // IOS_0226_自定义UIColectionView布局
  4. //
  5. // Created by ma c on 16/2/26.
  6. // Copyright © 2016年 博文科技. All rights reserved.
  7. //
  8.  
  9. #import "ViewController.h"
  10. #import "ImgCell.h"
  11. #import "LineLayout.h"
  12. #import "FoldLayout.h"
  13. #import "CircleLayout.h"
  14.  
  15. @interface ViewController ()<UICollectionViewDataSource,UICollectionViewDelegate>
  16.  
  17. @property (nonatomic, strong) NSMutableArray *imgsArray;
  18. @property (nonatomic, weak) UICollectionView *collectionView;
  19.  
  20. @end
  21.  
  22. @implementation ViewController
  23.  
  24. static NSString *ID = @"image";
  25.  
  26. - (NSMutableArray *)imgsArray
  27. {
  28. if (!_imgsArray) {
  29. _imgsArray = [NSMutableArray array];
  30.  
  31. for (int i=; i<; i++) {
  32. [_imgsArray addObject:[NSString stringWithFormat:@"%d.jpg",i]];
  33.  
  34. }
  35. }
  36. return _imgsArray;
  37. }
  38.  
  39. - (void)viewDidLoad {
  40. [super viewDidLoad];
  41. [self createUI];
  42. }
  43.  
  44. - (void)createUI
  45. {
  46. CGRect rect = CGRectMake(, , , );
  47. UICollectionView *collection = [[UICollectionView alloc] initWithFrame:rect collectionViewLayout:[[FoldLayout alloc] init]];
  48. collection.dataSource = self;
  49. collection.delegate = self;
  50. // [collection registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:ID];
  51. [collection registerNib:[UINib nibWithNibName:@"ImgCell" bundle:nil] forCellWithReuseIdentifier:ID];
  52. [self.view addSubview:collection];
  53. self.collectionView = collection;
  54.  
  55. }
  56.  
  57. - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
  58. {
  59. if ([self.collectionView.collectionViewLayout isKindOfClass:[FoldLayout class]]) {
  60. [self.collectionView setCollectionViewLayout:[[CircleLayout alloc] init] animated:YES];
  61. }
  62. else{
  63. [self.collectionView setCollectionViewLayout:[[FoldLayout alloc] init] animated:YES];
  64. }
  65. }
  66.  
  67. #pragma mark - UICollectionViewDataSource
  68.  
  69. - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
  70. {
  71. return self.imgsArray.count;
  72. }
  73.  
  74. - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
  75. {
  76. ImgCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:ID forIndexPath:indexPath];
  77.  
  78. cell.image = self.imgsArray[indexPath.item];
  79.  
  80. return cell;
  81. }
  82.  
  83. #pragma mark - UICollectionViewDelegate
  84.  
  85. - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
  86. {
  87. //删除模型数据
  88. [self.imgsArray removeObjectAtIndex:indexPath.item];
  89. //刷新UI
  90. [collectionView deleteItemsAtIndexPaths:@[indexPath]];
  91. }
  92.  
  93. @end
  1. LineLayout.m(线性流水布局)
  1. //
  2. // LineLayout.m
  3. // IOS_0226_自定义UIColectionView布局
  4. //
  5. // Created by ma c on 16/2/26.
  6. // Copyright © 2016年 博文科技. All rights reserved.
  7. //
  8.  
  9. #import "LineLayout.h"
  10.  
  11. static const CGFloat lineLayoutSize = ;
  12.  
  13. @implementation LineLayout
  14.  
  15. //初始化
  16. - (void)prepareLayout
  17. {
  18. [super prepareLayout];
  19. //设置item大小
  20. self.itemSize = CGSizeMake(lineLayoutSize, lineLayoutSize);
  21. //设置两端居中
  22. CGFloat inset = (self.collectionView.frame.size.width - lineLayoutSize) * 0.5;
  23. self.sectionInset = UIEdgeInsetsMake(, inset, , inset);
  24.  
  25. //设置水平滚动
  26. self.scrollDirection = UICollectionViewScrollDirectionHorizontal;
  27. //设置间距
  28. self.minimumLineSpacing = lineLayoutSize;
  29.  
  30. //每一个item都有自己的UICollectionViewLayoutAttributes
  31. //每一indexPath都有自己的UICollectionViewLayoutAttributes
  32. }
  33.  
  34. //只要显示的边界发生变化就重新布局:内部会重新调用prepareLayout,layoutAttributesForElementsInRect方法获得所有item的布局属性
  35. - (BOOL)shouldInvalidateLayoutForBoundsChange:(CGRect)newBounds
  36. {
  37. return YES;
  38. }
  39.  
  40. - (NSArray<UICollectionViewLayoutAttributes *> *)layoutAttributesForElementsInRect:(CGRect)rect
  41. {
  42. //NSLog(@"layoutAttributesForElementsInRect");
  43. // 0.计算可见的矩形框
  44. CGRect visibleRect;
  45. visibleRect.size = self.collectionView.frame.size;
  46. visibleRect.origin = self.collectionView.contentOffset;
  47. // 1.获得默认的item的UICollectionViewLayoutAttributes
  48. NSArray *array = [super layoutAttributesForElementsInRect:rect];
  49. NSArray * attributes = [[NSArray alloc] initWithArray:array copyItems:YES];
  50.  
  51. // 2.计算屏幕最中间的x
  52. CGFloat centerX = self.collectionView.contentOffset.x + self.collectionView.frame.size.width * 0.5;
  53.  
  54. for (UICollectionViewLayoutAttributes *attr in attributes) {
  55.  
  56. if (!CGRectIntersectsRect(visibleRect, attr.frame)) continue;
  57.  
  58. // 每一个item中点x
  59. CGFloat itemCenterX = attr.center.x;
  60. //计算item离屏幕中间的距离
  61. CGFloat distance = ABS(itemCenterX - centerX);
  62. //距离越小,缩放比例越大
  63. CGFloat zoom = - (distance / (self.collectionView.frame.size.width * 0.5));
  64. //NSLog(@"%f",zoom);
  65. //缩放比例
  66. CGFloat scale = + 0.5 * zoom;
  67. //缩放
  68. attr.transform3D = CATransform3DMakeScale(scale, scale, );
  69. }
  70.  
  71. return attributes;
  72. }
  73.  
  74. //用来设置UICollectionView停止滚动那一刻的位置
  75. - (CGPoint)targetContentOffsetForProposedContentOffset:(CGPoint)proposedContentOffset withScrollingVelocity:(CGPoint)velocity
  76. {
  77. // 1.计算UICollectionView最终停止的范围
  78. CGRect lastRect;
  79. lastRect.origin = proposedContentOffset;
  80. lastRect.size = self.collectionView.frame.size;
  81. // 计算屏幕中间的x
  82. CGFloat centerX = proposedContentOffset.x + self.collectionView.frame.size.width * 0.5;
  83. // 2.取出这个范围内的所有属性
  84. NSArray *array = [super layoutAttributesForElementsInRect:lastRect];
  85. NSArray * attributes = [[NSArray alloc] initWithArray:array copyItems:YES];
  86.  
  87. // 3.遍历所有属性
  88. CGFloat adjustOffsetX = MAXFLOAT;
  89. for (UICollectionViewLayoutAttributes *attr in attributes) {
  90. if (ABS(attr.center.x - centerX) < ABS(adjustOffsetX)) {
  91. adjustOffsetX = attr.center.x - centerX;
  92. }
  93. }
  94. return CGPointMake(proposedContentOffset.x + adjustOffsetX, proposedContentOffset.y);
  95. }
  96.  
  97. @end

FoldLayout.m(折叠布局)

  1. //
  2. // FoldLayout.m
  3. // IOS_0226_自定义UIColectionView布局
  4. //
  5. // Created by ma c on 16/2/27.
  6. // Copyright © 2016年 博文科技. All rights reserved.
  7. //
  8.  
  9. #import "FoldLayout.h"
  10.  
  11. #define Random0_1 (arc4random_uniform(100)/100)
  12.  
  13. @implementation FoldLayout
  14.  
  15. - (BOOL)shouldInvalidateLayoutForBoundsChange:(CGRect)newBounds
  16. {
  17. return YES;
  18. }
  19.  
  20. - (CGSize)collectionViewContentSize
  21. {
  22. return CGSizeMake(, );
  23. }
  24.  
  25. - (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath
  26. {
  27. NSArray *angles = @[@, @(-0.2), @(-0.5), @, @(0.2), @(0.5)];
  28.  
  29. UICollectionViewLayoutAttributes *attrs = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:indexPath];
  30. attrs.size = CGSizeMake(, );
  31. // attrs.center = CGPointMake(arc4random_uniform(self.collectionView.frame.size.width), arc4random_uniform(self.collectionView.frame.size.height));
  32. attrs.center = CGPointMake(self.collectionView.frame.size.width * 0.5, self.collectionView.frame.size.height * 0.5);
  33. if (indexPath.item >= ) {
  34. attrs.hidden = YES;
  35. } else {
  36. attrs.transform = CGAffineTransformMakeRotation([angles[indexPath.item] floatValue]);
  37. //zIndex越大,就越在上面
  38. attrs.zIndex = [self.collectionView numberOfItemsInSection:indexPath.section] - indexPath.item;
  39. }
  40. return attrs;
  41. }
  42.  
  43. - (NSArray<UICollectionViewLayoutAttributes *> *)layoutAttributesForElementsInRect:(CGRect)rect
  44. {
  45. NSMutableArray *array = [NSMutableArray array];
  46. NSInteger count = [self.collectionView numberOfItemsInSection:];
  47.  
  48. for (int i=; i<count; i++) {
  49. // UICollectionViewLayoutAttributes *attrs = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:[NSIndexPath indexPathForItem:i inSection:0]];
  50. // attrs.size = CGSizeMake(100, 100);
  51. // attrs.center = CGPointMake(self.collectionView.frame.size.width * 0.5, self.collectionView.frame.size.height * 0.5);
  52. // if (i >= 5) {
  53. // attrs.hidden = YES;
  54. // } else {
  55. //
  56. // NSArray *angles = @[@0, @(-0.2), @(-0.5), @(0.2), @(0.5)];
  57. // attrs.transform = CGAffineTransformMakeRotation([angles[i] floatValue]);
  58. // //zIndex越大,就越在上面
  59. // attrs.zIndex = count - i;
  60. NSIndexPath *indexPath = [NSIndexPath indexPathForItem:i inSection:];
  61. UICollectionViewLayoutAttributes *attrs = [self layoutAttributesForItemAtIndexPath:indexPath];
  62. [array addObject:attrs];
  63.  
  64. }
  65. return array;
  66. }
  67.  
  68. @end
  1. CircleLayout.m(环形布局)
  1. //
  2. // CircleLayout.m
  3. // IOS_0226_自定义UIColectionView布局
  4. //
  5. // Created by ma c on 16/2/27.
  6. // Copyright © 2016年 博文科技. All rights reserved.
  7. //
  8.  
  9. #import "CircleLayout.h"
  10.  
  11. @implementation CircleLayout
  12.  
  13. - (BOOL)shouldInvalidateLayoutForBoundsChange:(CGRect)newBounds
  14. {
  15. return YES;
  16. }
  17.  
  18. - (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath
  19. {
  20.  
  21. UICollectionViewLayoutAttributes *attrs = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:indexPath];
  22. attrs.size = CGSizeMake(, );
  23.  
  24. //圆的半径
  25. CGFloat radius = ;
  26. CGPoint cireclecenter = CGPointMake(self.collectionView.frame.size.width * 0.5, self.collectionView.frame.size.height * 0.5);
  27. //每个item间角度
  28. CGFloat angleDetla = M_PI * / [self.collectionView numberOfItemsInSection:indexPath.section];
  29.  
  30. //计算当前item角度
  31. CGFloat angle = indexPath.item * angleDetla;
  32. attrs.center = CGPointMake(cireclecenter.x + radius * cosf(angle), cireclecenter.y + radius * sinf(angle));
  33.  
  34. attrs.zIndex = indexPath.item;
  35. return attrs;
  36. }
  37.  
  38. - (NSArray<UICollectionViewLayoutAttributes *> *)layoutAttributesForElementsInRect:(CGRect)rect
  39. {
  40. NSMutableArray *array = [NSMutableArray array];
  41. NSInteger count = [self.collectionView numberOfItemsInSection:];
  42.  
  43. for (int i=; i<count; i++) {
  44. NSIndexPath *indexPath = [NSIndexPath indexPathForItem:i inSection:];
  45. UICollectionViewLayoutAttributes *attrs = [self layoutAttributesForItemAtIndexPath:indexPath];
  46. [array addObject:attrs];
  47.  
  48. }
  49. return array;
  50. }
  51.  
  52. @end
  1. ImgCell.h(自定义UICollectionViewCell
  1. //
  2. // ImgCell.h
  3. // IOS_0226_自定义UIColectionView布局
  4. //
  5. // Created by ma c on 16/2/26.
  6. // Copyright © 2016年 博文科技. All rights reserved.
  7. //
  8.  
  9. #import <UIKit/UIKit.h>
  10.  
  11. @interface ImgCell : UICollectionViewCell
  12.  
  13. @property (weak, nonatomic) IBOutlet UIImageView *imgView;
  14. @property (nonatomic, copy) NSString *image;
  15.  
  16. @end
  17.  
  18. //
  19. // ImgCell.m
  20. // IOS_0226_自定义UIColectionView布局
  21. //
  22. // Created by ma c on 16/2/26.
  23. // Copyright © 2016年 博文科技. All rights reserved.
  24. //
  25.  
  26. #import "ImgCell.h"
  27.  
  28. @interface ImgCell ()
  29.  
  30. @end
  31.  
  32. @implementation ImgCell
  33.  
  34. - (void)setImage:(NSString *)image
  35. {
  36. _image = [image copy];
  37. self.imgView.image = [UIImage imageNamed:_image];
  38. }
  39.  
  40. - (void)awakeFromNib {
  41. self.imgView.layer.borderWidth = ;
  42. self.imgView.layer.borderColor = [UIColor whiteColor].CGColor;
  43. self.imgView.layer.cornerRadius = ;
  44. self.imgView.clipsToBounds = YES;
  45. }
  46.  
  47. @end

IOS UI-自定义UIColectionView布局的更多相关文章

  1. iOS开发自定义流水布局

    //集成UICollectionViewFlowLayout 自己写的布局 //  SJBFlowLayout.m //  自定义流水布局 // //  Created by zyyt on 16/7 ...

  2. Android开发1:基本UI界面设计——布局和组件

    前言 啦啦啦~本学期要开始学习Android开发啦~ 博主在开始学习前是完完全全的小白,只有在平时完成老师要求的实验的过程中一步一步学习~从此篇博文起,博主将开始发布Android开发有关的博文,希望 ...

  3. [前端]使用JQuery UI Layout Plug-in布局 - wolfy

    引言 使用JQuery UI Layout Plug-in布局框架实现快速布局,用起来还是挺方便的,稍微研究了一下,就能上手,关于该布局框架的材料,网上也挺多的.在项目中也使用到了,不过那是前端的工作 ...

  4. JQuery UI Layout Plug-in布局

    端]使用JQuery UI Layout Plug-in布局   引言 使用JQuery UI Layout Plug-in布局框架实现快速布局,用起来还是挺方便的,稍微研究了一下,就能上手,关于该布 ...

  5. 【详细】Android入门到放弃篇-YES OR NO-》各种UI组件,布局管理器,单元Activity

    问:达叔,你放弃了吗? 答:不,放弃是不可能的,丢了Android,你会心疼吗?如果别人把你丢掉,你是痛苦呢?还是痛苦呢?~ 引导语 有人说,爱上一个人是痛苦的,有人说,喜欢一个人是幸福的. 人与人之 ...

  6. [前端]使用JQuery UI Layout Plug-in布局

    引言 使用JQuery UI Layout Plug-in布局框架实现快速布局,用起来还是挺方便的,稍微研究了一下,就能上手,关于该布局框架的材料,网上也挺多的.在项目中也使用到了,不过那是前端的工作 ...

  7. iOS 如何自定义UISearchBar 中textField的高度

    iOS 如何自定义UISearchBar 中textField的高度 只需设置下边的方法就可以 [_searchBar setSearchFieldBackgroundImage:[UIImage i ...

  8. 原生HTML5 input type=file按钮UI自定义

    原生<input type="file" name="file" />长得太丑 提升一下颜值 实现方案一.设置input[type=file]透明度 ...

  9. iOS 隐藏自定义tabbar

    iOS  隐藏自定义tabbar -(void)viewWillAppear:(BOOL)animated { NSArray *array=self.tabBarController.view.su ...

随机推荐

  1. VirtualBox AndroidX86 网络设置

    在Virtualbox中,把虚拟机网络设为“网络地址转换(NAT)”模式,高级中控制芯片(T)选择:PCnet-FAST III(Am79C973), 然后启动你的android-x86 4.0虚拟机 ...

  2. Zen-cart产品页面随机调用Wordpress文章

    <?php require('./wordpress所在目录/wp-blog-header.php'); ?><?php$rand_posts = get_posts('number ...

  3. html5的常用函数

    required       验证非空 readonly      文本只读 video          视频播放标签 Ogg            带有 Theora 视频编码和 Vorbis 音 ...

  4. 给idea配置默认的maven

    一.配置Maven环境 1.下载apache-maven文件,选择自己需要的版本,地址: http://mirror.bit.edu.cn/apache/maven/maven-3/3.5.0/bin ...

  5. Django Restful API Class Based View

    基于class定义view 前言: 我们首先通过以class的方式重写view,我们可以自己构造类也可以通过res_framework 提供的mixins和generics类库直接构造类 下面来看下自 ...

  6. django之路由(url)

    前言: Django大致工作流程 1.客户端发送请求(get/post)经过web服务器.Django中间件. 到达路由分配系统 2.路由分配系统根据提取 request中携带的的url路径(path ...

  7. Winter-1-F Number Sequence 解题报告及测试数据

    Time Limit:1000MS     Memory Limit:32768KB Description ​A number sequence is defined as follows:f(1) ...

  8. Java并发编程之CountDownLatch的用法

    一.含义 CountDownLatch类位于java.util.concurrent包下,利用它可以实现类似计数器的功能.CountDownLatch是一个同步的辅助类,它可以允许一个或多个线程等待, ...

  9. zookeeper 监听事件 NodeCacheListener

    zookeeper 监听事件 NodeCacheListener NodeCacheListener一次注册,每次监听,但是监听不到操作类型,不知道是增加?删除?还是修改? 1.测试类: packag ...

  10. ElasticSearch(四) ElasticSearch中文分词插件IK的简单测试

    先来一个简单的测试 # curl -XPOST "http://192.168.9.155:9200/_analyze?analyzer=standard&pretty" ...