IOS UI-自定义UIColectionView布局
ViewController.m
- //
- // ViewController.m
- // IOS_0226_自定义UIColectionView布局
- //
- // Created by ma c on 16/2/26.
- // Copyright © 2016年 博文科技. All rights reserved.
- //
- #import "ViewController.h"
- #import "ImgCell.h"
- #import "LineLayout.h"
- #import "FoldLayout.h"
- #import "CircleLayout.h"
- @interface ViewController ()<UICollectionViewDataSource,UICollectionViewDelegate>
- @property (nonatomic, strong) NSMutableArray *imgsArray;
- @property (nonatomic, weak) UICollectionView *collectionView;
- @end
- @implementation ViewController
- static NSString *ID = @"image";
- - (NSMutableArray *)imgsArray
- {
- if (!_imgsArray) {
- _imgsArray = [NSMutableArray array];
- for (int i=; i<; i++) {
- [_imgsArray addObject:[NSString stringWithFormat:@"%d.jpg",i]];
- }
- }
- return _imgsArray;
- }
- - (void)viewDidLoad {
- [super viewDidLoad];
- [self createUI];
- }
- - (void)createUI
- {
- CGRect rect = CGRectMake(, , , );
- UICollectionView *collection = [[UICollectionView alloc] initWithFrame:rect collectionViewLayout:[[FoldLayout alloc] init]];
- collection.dataSource = self;
- collection.delegate = self;
- // [collection registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:ID];
- [collection registerNib:[UINib nibWithNibName:@"ImgCell" bundle:nil] forCellWithReuseIdentifier:ID];
- [self.view addSubview:collection];
- self.collectionView = collection;
- }
- - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
- {
- if ([self.collectionView.collectionViewLayout isKindOfClass:[FoldLayout class]]) {
- [self.collectionView setCollectionViewLayout:[[CircleLayout alloc] init] animated:YES];
- }
- else{
- [self.collectionView setCollectionViewLayout:[[FoldLayout alloc] init] animated:YES];
- }
- }
- #pragma mark - UICollectionViewDataSource
- - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
- {
- return self.imgsArray.count;
- }
- - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
- {
- ImgCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:ID forIndexPath:indexPath];
- cell.image = self.imgsArray[indexPath.item];
- return cell;
- }
- #pragma mark - UICollectionViewDelegate
- - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
- {
- //删除模型数据
- [self.imgsArray removeObjectAtIndex:indexPath.item];
- //刷新UI
- [collectionView deleteItemsAtIndexPaths:@[indexPath]];
- }
- @end
- LineLayout.m(线性流水布局)
- //
- // LineLayout.m
- // IOS_0226_自定义UIColectionView布局
- //
- // Created by ma c on 16/2/26.
- // Copyright © 2016年 博文科技. All rights reserved.
- //
- #import "LineLayout.h"
- static const CGFloat lineLayoutSize = ;
- @implementation LineLayout
- //初始化
- - (void)prepareLayout
- {
- [super prepareLayout];
- //设置item大小
- self.itemSize = CGSizeMake(lineLayoutSize, lineLayoutSize);
- //设置两端居中
- CGFloat inset = (self.collectionView.frame.size.width - lineLayoutSize) * 0.5;
- self.sectionInset = UIEdgeInsetsMake(, inset, , inset);
- //设置水平滚动
- self.scrollDirection = UICollectionViewScrollDirectionHorizontal;
- //设置间距
- self.minimumLineSpacing = lineLayoutSize;
- //每一个item都有自己的UICollectionViewLayoutAttributes
- //每一indexPath都有自己的UICollectionViewLayoutAttributes
- }
- //只要显示的边界发生变化就重新布局:内部会重新调用prepareLayout,layoutAttributesForElementsInRect方法获得所有item的布局属性
- - (BOOL)shouldInvalidateLayoutForBoundsChange:(CGRect)newBounds
- {
- return YES;
- }
- - (NSArray<UICollectionViewLayoutAttributes *> *)layoutAttributesForElementsInRect:(CGRect)rect
- {
- //NSLog(@"layoutAttributesForElementsInRect");
- // 0.计算可见的矩形框
- CGRect visibleRect;
- visibleRect.size = self.collectionView.frame.size;
- visibleRect.origin = self.collectionView.contentOffset;
- // 1.获得默认的item的UICollectionViewLayoutAttributes
- NSArray *array = [super layoutAttributesForElementsInRect:rect];
- NSArray * attributes = [[NSArray alloc] initWithArray:array copyItems:YES];
- // 2.计算屏幕最中间的x
- CGFloat centerX = self.collectionView.contentOffset.x + self.collectionView.frame.size.width * 0.5;
- for (UICollectionViewLayoutAttributes *attr in attributes) {
- if (!CGRectIntersectsRect(visibleRect, attr.frame)) continue;
- // 每一个item中点x
- CGFloat itemCenterX = attr.center.x;
- //计算item离屏幕中间的距离
- CGFloat distance = ABS(itemCenterX - centerX);
- //距离越小,缩放比例越大
- CGFloat zoom = - (distance / (self.collectionView.frame.size.width * 0.5));
- //NSLog(@"%f",zoom);
- //缩放比例
- CGFloat scale = + 0.5 * zoom;
- //缩放
- attr.transform3D = CATransform3DMakeScale(scale, scale, );
- }
- return attributes;
- }
- //用来设置UICollectionView停止滚动那一刻的位置
- - (CGPoint)targetContentOffsetForProposedContentOffset:(CGPoint)proposedContentOffset withScrollingVelocity:(CGPoint)velocity
- {
- // 1.计算UICollectionView最终停止的范围
- CGRect lastRect;
- lastRect.origin = proposedContentOffset;
- lastRect.size = self.collectionView.frame.size;
- // 计算屏幕中间的x
- CGFloat centerX = proposedContentOffset.x + self.collectionView.frame.size.width * 0.5;
- // 2.取出这个范围内的所有属性
- NSArray *array = [super layoutAttributesForElementsInRect:lastRect];
- NSArray * attributes = [[NSArray alloc] initWithArray:array copyItems:YES];
- // 3.遍历所有属性
- CGFloat adjustOffsetX = MAXFLOAT;
- for (UICollectionViewLayoutAttributes *attr in attributes) {
- if (ABS(attr.center.x - centerX) < ABS(adjustOffsetX)) {
- adjustOffsetX = attr.center.x - centerX;
- }
- }
- return CGPointMake(proposedContentOffset.x + adjustOffsetX, proposedContentOffset.y);
- }
- @end
FoldLayout.m(折叠布局)
- //
- // FoldLayout.m
- // IOS_0226_自定义UIColectionView布局
- //
- // Created by ma c on 16/2/27.
- // Copyright © 2016年 博文科技. All rights reserved.
- //
- #import "FoldLayout.h"
- #define Random0_1 (arc4random_uniform(100)/100)
- @implementation FoldLayout
- - (BOOL)shouldInvalidateLayoutForBoundsChange:(CGRect)newBounds
- {
- return YES;
- }
- - (CGSize)collectionViewContentSize
- {
- return CGSizeMake(, );
- }
- - (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath
- {
- NSArray *angles = @[@, @(-0.2), @(-0.5), @, @(0.2), @(0.5)];
- UICollectionViewLayoutAttributes *attrs = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:indexPath];
- attrs.size = CGSizeMake(, );
- // attrs.center = CGPointMake(arc4random_uniform(self.collectionView.frame.size.width), arc4random_uniform(self.collectionView.frame.size.height));
- attrs.center = CGPointMake(self.collectionView.frame.size.width * 0.5, self.collectionView.frame.size.height * 0.5);
- if (indexPath.item >= ) {
- attrs.hidden = YES;
- } else {
- attrs.transform = CGAffineTransformMakeRotation([angles[indexPath.item] floatValue]);
- //zIndex越大,就越在上面
- attrs.zIndex = [self.collectionView numberOfItemsInSection:indexPath.section] - indexPath.item;
- }
- return attrs;
- }
- - (NSArray<UICollectionViewLayoutAttributes *> *)layoutAttributesForElementsInRect:(CGRect)rect
- {
- NSMutableArray *array = [NSMutableArray array];
- NSInteger count = [self.collectionView numberOfItemsInSection:];
- for (int i=; i<count; i++) {
- // UICollectionViewLayoutAttributes *attrs = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:[NSIndexPath indexPathForItem:i inSection:0]];
- // attrs.size = CGSizeMake(100, 100);
- // attrs.center = CGPointMake(self.collectionView.frame.size.width * 0.5, self.collectionView.frame.size.height * 0.5);
- // if (i >= 5) {
- // attrs.hidden = YES;
- // } else {
- //
- // NSArray *angles = @[@0, @(-0.2), @(-0.5), @(0.2), @(0.5)];
- // attrs.transform = CGAffineTransformMakeRotation([angles[i] floatValue]);
- // //zIndex越大,就越在上面
- // attrs.zIndex = count - i;
- NSIndexPath *indexPath = [NSIndexPath indexPathForItem:i inSection:];
- UICollectionViewLayoutAttributes *attrs = [self layoutAttributesForItemAtIndexPath:indexPath];
- [array addObject:attrs];
- }
- return array;
- }
- @end
- CircleLayout.m(环形布局)
- //
- // CircleLayout.m
- // IOS_0226_自定义UIColectionView布局
- //
- // Created by ma c on 16/2/27.
- // Copyright © 2016年 博文科技. All rights reserved.
- //
- #import "CircleLayout.h"
- @implementation CircleLayout
- - (BOOL)shouldInvalidateLayoutForBoundsChange:(CGRect)newBounds
- {
- return YES;
- }
- - (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath
- {
- UICollectionViewLayoutAttributes *attrs = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:indexPath];
- attrs.size = CGSizeMake(, );
- //圆的半径
- CGFloat radius = ;
- CGPoint cireclecenter = CGPointMake(self.collectionView.frame.size.width * 0.5, self.collectionView.frame.size.height * 0.5);
- //每个item间角度
- CGFloat angleDetla = M_PI * / [self.collectionView numberOfItemsInSection:indexPath.section];
- //计算当前item角度
- CGFloat angle = indexPath.item * angleDetla;
- attrs.center = CGPointMake(cireclecenter.x + radius * cosf(angle), cireclecenter.y + radius * sinf(angle));
- attrs.zIndex = indexPath.item;
- return attrs;
- }
- - (NSArray<UICollectionViewLayoutAttributes *> *)layoutAttributesForElementsInRect:(CGRect)rect
- {
- NSMutableArray *array = [NSMutableArray array];
- NSInteger count = [self.collectionView numberOfItemsInSection:];
- for (int i=; i<count; i++) {
- NSIndexPath *indexPath = [NSIndexPath indexPathForItem:i inSection:];
- UICollectionViewLayoutAttributes *attrs = [self layoutAttributesForItemAtIndexPath:indexPath];
- [array addObject:attrs];
- }
- return array;
- }
- @end
- ImgCell.h(自定义UICollectionViewCell)
- //
- // ImgCell.h
- // IOS_0226_自定义UIColectionView布局
- //
- // Created by ma c on 16/2/26.
- // Copyright © 2016年 博文科技. All rights reserved.
- //
- #import <UIKit/UIKit.h>
- @interface ImgCell : UICollectionViewCell
- @property (weak, nonatomic) IBOutlet UIImageView *imgView;
- @property (nonatomic, copy) NSString *image;
- @end
- //
- // ImgCell.m
- // IOS_0226_自定义UIColectionView布局
- //
- // Created by ma c on 16/2/26.
- // Copyright © 2016年 博文科技. All rights reserved.
- //
- #import "ImgCell.h"
- @interface ImgCell ()
- @end
- @implementation ImgCell
- - (void)setImage:(NSString *)image
- {
- _image = [image copy];
- self.imgView.image = [UIImage imageNamed:_image];
- }
- - (void)awakeFromNib {
- self.imgView.layer.borderWidth = ;
- self.imgView.layer.borderColor = [UIColor whiteColor].CGColor;
- self.imgView.layer.cornerRadius = ;
- self.imgView.clipsToBounds = YES;
- }
- @end
IOS UI-自定义UIColectionView布局的更多相关文章
- iOS开发自定义流水布局
//集成UICollectionViewFlowLayout 自己写的布局 // SJBFlowLayout.m // 自定义流水布局 // // Created by zyyt on 16/7 ...
- Android开发1:基本UI界面设计——布局和组件
前言 啦啦啦~本学期要开始学习Android开发啦~ 博主在开始学习前是完完全全的小白,只有在平时完成老师要求的实验的过程中一步一步学习~从此篇博文起,博主将开始发布Android开发有关的博文,希望 ...
- [前端]使用JQuery UI Layout Plug-in布局 - wolfy
引言 使用JQuery UI Layout Plug-in布局框架实现快速布局,用起来还是挺方便的,稍微研究了一下,就能上手,关于该布局框架的材料,网上也挺多的.在项目中也使用到了,不过那是前端的工作 ...
- JQuery UI Layout Plug-in布局
端]使用JQuery UI Layout Plug-in布局 引言 使用JQuery UI Layout Plug-in布局框架实现快速布局,用起来还是挺方便的,稍微研究了一下,就能上手,关于该布 ...
- 【详细】Android入门到放弃篇-YES OR NO-》各种UI组件,布局管理器,单元Activity
问:达叔,你放弃了吗? 答:不,放弃是不可能的,丢了Android,你会心疼吗?如果别人把你丢掉,你是痛苦呢?还是痛苦呢?~ 引导语 有人说,爱上一个人是痛苦的,有人说,喜欢一个人是幸福的. 人与人之 ...
- [前端]使用JQuery UI Layout Plug-in布局
引言 使用JQuery UI Layout Plug-in布局框架实现快速布局,用起来还是挺方便的,稍微研究了一下,就能上手,关于该布局框架的材料,网上也挺多的.在项目中也使用到了,不过那是前端的工作 ...
- iOS 如何自定义UISearchBar 中textField的高度
iOS 如何自定义UISearchBar 中textField的高度 只需设置下边的方法就可以 [_searchBar setSearchFieldBackgroundImage:[UIImage i ...
- 原生HTML5 input type=file按钮UI自定义
原生<input type="file" name="file" />长得太丑 提升一下颜值 实现方案一.设置input[type=file]透明度 ...
- iOS 隐藏自定义tabbar
iOS 隐藏自定义tabbar -(void)viewWillAppear:(BOOL)animated { NSArray *array=self.tabBarController.view.su ...
随机推荐
- VirtualBox AndroidX86 网络设置
在Virtualbox中,把虚拟机网络设为“网络地址转换(NAT)”模式,高级中控制芯片(T)选择:PCnet-FAST III(Am79C973), 然后启动你的android-x86 4.0虚拟机 ...
- Zen-cart产品页面随机调用Wordpress文章
<?php require('./wordpress所在目录/wp-blog-header.php'); ?><?php$rand_posts = get_posts('number ...
- html5的常用函数
required 验证非空 readonly 文本只读 video 视频播放标签 Ogg 带有 Theora 视频编码和 Vorbis 音 ...
- 给idea配置默认的maven
一.配置Maven环境 1.下载apache-maven文件,选择自己需要的版本,地址: http://mirror.bit.edu.cn/apache/maven/maven-3/3.5.0/bin ...
- Django Restful API Class Based View
基于class定义view 前言: 我们首先通过以class的方式重写view,我们可以自己构造类也可以通过res_framework 提供的mixins和generics类库直接构造类 下面来看下自 ...
- django之路由(url)
前言: Django大致工作流程 1.客户端发送请求(get/post)经过web服务器.Django中间件. 到达路由分配系统 2.路由分配系统根据提取 request中携带的的url路径(path ...
- Winter-1-F Number Sequence 解题报告及测试数据
Time Limit:1000MS Memory Limit:32768KB Description A number sequence is defined as follows:f(1) ...
- Java并发编程之CountDownLatch的用法
一.含义 CountDownLatch类位于java.util.concurrent包下,利用它可以实现类似计数器的功能.CountDownLatch是一个同步的辅助类,它可以允许一个或多个线程等待, ...
- zookeeper 监听事件 NodeCacheListener
zookeeper 监听事件 NodeCacheListener NodeCacheListener一次注册,每次监听,但是监听不到操作类型,不知道是增加?删除?还是修改? 1.测试类: packag ...
- ElasticSearch(四) ElasticSearch中文分词插件IK的简单测试
先来一个简单的测试 # curl -XPOST "http://192.168.9.155:9200/_analyze?analyzer=standard&pretty" ...