UICollectionView之自定义Layout
#import <UIKit/UIKit.h>
@interface WQViewController : UIViewController
- (id)initWithFrame:(CGRect)frame;
@end
#import "WQViewController.h"
#import "WQCollectionViewController.h"
#import "WQCollectionViewCircleLayout.h"
@interface WQViewController ()
@end
@implementation WQViewController
- (id)initWithFrame:(CGRect)frame {
if (self == [super init]) {
self.view.frame = frame;
self.view.backgroundColor = [UIColor yellowColor];
}
return self;
}
- (void)viewDidLoad {
[super viewDidLoad];
WQCollectionViewCircleLayout *circleLayout = [[WQCollectionViewCircleLayout alloc] init];
WQCollectionViewController *collectionVC = [[WQCollectionViewController alloc] initWithCollectionViewLayout:circleLayout];
[self addChildViewController:collectionVC];
[self.view addSubview:collectionVC.collectionView];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
~~~~~~~~~~~~~~~~~~~~~~~
#import <UIKit/UIKit.h>
@interface WQCollectionViewController : UICollectionViewController
@end
#import "WQCollectionViewController.h"
#import "WQCollectionViewCircleLayout.h"
#import "WQCollectionViewCell.h"
@interface WQCollectionViewController ()
@property (assign, nonatomic) NSInteger cellCount;
@end
@implementation WQCollectionViewController
static NSString * const reuseIdentifier = @"Cell";
- (void)viewDidLoad {
[super viewDidLoad];
_cellCount = 8;
self.collectionView.frame = [UIScreen mainScreen].bounds;
self.collectionView.backgroundColor = [UIColor whiteColor];
[self.collectionView registerClass:[WQCollectionViewCell class] forCellWithReuseIdentifier:reuseIdentifier];
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTapGesture:)];
[self.collectionView addGestureRecognizer:tapGesture];
}
#pragma mark - UITapGesture
- (void)handleTapGesture:(UITapGestureRecognizer *)sender {
if (sender.state == UIGestureRecognizerStateEnded) {
CGPoint initiailPinchPoint = [sender locationInView:self.collectionView];
NSIndexPath *tapCellPath = [self.collectionView indexPathForItemAtPoint:initiailPinchPoint];
if (tapCellPath != nil) {
[self.collectionView performBatchUpdates:^{
self.cellCount = self.cellCount - 1;
[self.collectionView deleteItemsAtIndexPaths:[NSArray arrayWithObject:tapCellPath]];
} completion:^(BOOL finished) {
[self.collectionView reloadData];
}];
} else {
[self.collectionView performBatchUpdates:^{
self.cellCount = self.cellCount + 1;
[self.collectionView insertItemsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForItem:(random()%self.cellCount) inSection:0]]];
} completion:^(BOOL finished) {
[self.collectionView reloadData];
}];
}
}
}
#pragma mark <UICollectionViewDataSource>
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return _cellCount;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
WQCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
[cell updateAppreanceAccrodingToItem:indexPath.item];
cell.contentView.backgroundColor = [UIColor redColor];
cell.contentView.layer.cornerRadius = 20;
cell.contentView.layer.masksToBounds = YES;
// UILabel *lbl = [[UILabel alloc] init];
// lbl.center = cell.contentView.center;
// lbl.text = [NSString stringWithFormat:@"%ld", indexPath.item];
// [lbl sizeToFit];
// [cell.contentView addSubview:lbl];
//
return cell;
}
#pragma mark <UICollectionViewDelegate>
/*
// Uncomment this method to specify if the specified item should be highlighted during tracking
- (BOOL)collectionView:(UICollectionView *)collectionView shouldHighlightItemAtIndexPath:(NSIndexPath *)indexPath {
return YES;
}
*/
/*
// Uncomment this method to specify if the specified item should be selected
- (BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath {
return YES;
}
*/
/*
// Uncomment these methods to specify if an action menu should be displayed for the specified item, and react to actions performed on the item
- (BOOL)collectionView:(UICollectionView *)collectionView shouldShowMenuForItemAtIndexPath:(NSIndexPath *)indexPath {
return NO;
}
- (BOOL)collectionView:(UICollectionView *)collectionView canPerformAction:(SEL)action forItemAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender {
return NO;
}
- (void)collectionView:(UICollectionView *)collectionView performAction:(SEL)action forItemAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender {
}
*/
@end
~~~~~~~~~~~~~~~~~~~~~~~~
#import <UIKit/UIKit.h>
@interface WQCollectionViewCell : UICollectionViewCell
- (void)updateAppreanceAccrodingToItem:(NSInteger)item;
@end
#import "WQCollectionViewCell.h"
@interface WQCollectionViewCell ()
@property(strong, nonatomic)UILabel *lbl;
@end
@implementation WQCollectionViewCell
-(void)prepareForReuse {
_lbl.text = nil;
}
- (void)updateAppreanceAccrodingToItem:(NSInteger)item {
self.lbl.text = [NSString stringWithFormat:@"%ld", item];
[self addSubview:self.lbl];
}
- (UILabel *)lbl {
if (_lbl == nil) {
_lbl = [[UILabel alloc] init];
[_lbl setFont:[UIFont systemFontOfSize:12.0f]];
_lbl.textAlignment = NSTextAlignmentCenter;
_lbl.frame = CGRectMake(10, 10, 20, 20);
}
return _lbl;
}
@end
~~~~~~~~~~~~~~~~~~~~~自定义layout
#import <UIKit/UIKit.h>
@interface WQCollectionViewCircleLayout : UICollectionViewLayout
@end
#define ITEM_SIZE 40.0f
#import "WQCollectionViewCircleLayout.h"
@interface WQCollectionViewCircleLayout ()
@property(assign, nonatomic) NSInteger cellCount;
@property(assign, nonatomic) CGPoint center;
@property(assign, nonatomic) CGFloat radius;
@property(strong, nonatomic) NSMutableArray *indexPathToAnimation;
@end
@implementation WQCollectionViewCircleLayout
- (void)prepareLayout {
[super prepareLayout];
CGSize size = self.collectionView.frame.size;
_cellCount = [self.collectionView numberOfItemsInSection:0];
_center = CGPointMake(size.width/2.0f, size.height/2.0f);
// _radius = MIN(size.width/2.5f, size.height/2.5f);
_radius = 120;
}
- (CGSize)collectionViewContentSize {
return self.collectionView.frame.size;
}
- (NSArray<UICollectionViewLayoutAttributes *> *)layoutAttributesForElementsInRect:(CGRect)rect {
NSMutableArray *attributes = [NSMutableArray array];
for (int i = 0; i < _cellCount; i ++) {
NSIndexPath *indexPath = [NSIndexPath indexPathForItem:i inSection:0];
[attributes addObject:[self layoutAttributesForItemAtIndexPath:indexPath]];
}
return attributes;
}
-(BOOL)shouldInvalidateLayoutForBoundsChange:(CGRect)newBounds {
CGRect oldBounds = self.collectionView.bounds;
if (CGRectGetWidth(newBounds) != CGRectGetWidth(oldBounds) || CGRectGetHeight(newBounds) != CGRectGetWidth(oldBounds)) {
return YES;
}
return NO;
}
- (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath {
UICollectionViewLayoutAttributes *layoutAttributes = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:indexPath];
layoutAttributes.size = CGSizeMake(ITEM_SIZE, ITEM_SIZE);
layoutAttributes.center = CGPointMake(_center.x + _radius * cosf(2*indexPath.item*M_PI / _cellCount), _center.y + _radius * sinf(2*indexPath.item*M_PI / _cellCount));
NSLog(@"%@", NSStringFromCGRect(layoutAttributes.frame));
return layoutAttributes;
}
-(void)prepareForCollectionViewUpdates:(NSArray<UICollectionViewUpdateItem *> *)updateItems {
[super prepareForCollectionViewUpdates:updateItems];
NSMutableArray *attrsArray = [NSMutableArray array];
for (UICollectionViewUpdateItem *updateItem in updateItems) {
switch (updateItem.updateAction) {
case UICollectionUpdateActionInsert:
{
NSIndexPath *itemIndex = updateItem.indexPathAfterUpdate;
[attrsArray addObject:itemIndex];
}
break;
case UICollectionUpdateActionDelete:
{
NSIndexPath *itemIndex = updateItem.indexPathBeforeUpdate;
[attrsArray addObject:itemIndex];
}
break;
case UICollectionUpdateActionMove:
{
NSIndexPath *itemIndexBeforeUpdate = updateItem.indexPathBeforeUpdate;
NSIndexPath *itemIndexAfterUpdate = updateItem.indexPathAfterUpdate;
[attrsArray addObject:itemIndexBeforeUpdate];
[attrsArray addObject:itemIndexAfterUpdate];
}
break;
case UICollectionUpdateActionReload:
break;
case UICollectionUpdateActionNone:
break;
default:
break;
}
}
_indexPathToAnimation = attrsArray;
}
-(UICollectionViewLayoutAttributes *)initialLayoutAttributesForAppearingItemAtIndexPath:(NSIndexPath *)itemIndexPath {
UICollectionViewLayoutAttributes *layoutAttr = [self layoutAttributesForItemAtIndexPath:itemIndexPath];
if ([_indexPathToAnimation containsObject:itemIndexPath]) {
layoutAttr.transform = CGAffineTransformRotate(CGAffineTransformMakeScale(0.2, 0.2), M_PI);
layoutAttr.center = CGPointMake(self.collectionView.center.x, self.collectionView.bounds.size.height);
// layoutAttr.center = CGPointMake(CGRectGetMidX(self.collectionView.bounds), CGRectGetMidY(self.collectionView.bounds));
[self.indexPathToAnimation removeObject:itemIndexPath];
}
return layoutAttr;
}
@end
~~~~~~~~~~~~~实现效果:
UICollectionView之自定义Layout的更多相关文章
- UICollectionView Layout自定义 Layout布局
from: http://www.tuicool.com/articles/vuyIriN 当我们使用系统自带的UICollectionViewFlowLayout无法实现我们的布局时,我们就可以 ...
- 自定义 Layout布局 UICollectionViewLayout
from: http://www.tuicool.com/articles/vuyIriN 当我们使用系统自带的UICollectionViewFlowLayout无法实现我们的布局时,我们就可以 ...
- 一个UICollectionView自定义layout的实现
#import <UIKit/UIKit.h> @interface AppDelegate : UIResponder <UIApplicationDelegate> @ ...
- UICollectionView的水平流水布局自定义layout
最近做合创共美的商城项目,遇到发货地址的不配送地区,是做一个弹出框,弹出框的布局是根据地名字数长短不齐的标签. 本来也可以用tableview来做的.只不过多建几个tableviewcell就可以了. ...
- log4net 自定义Layout日志字段
最近在使用log4net的时候有一个简单的需求,就是自定义个格式化输出符.这个输出符是专门用来帮我记录下业务ID.业务类型的.比如,“businessID:328593,businessType: o ...
- UICollectionview实现自定义cell的移动删除
今天 ,有群里人询问了 ,支付宝首页的UICollectionview 的cell(其实不能成为cell,应该是item,不过大家习惯这么称呼了)怎么实现 自定义的拖拽 和删除,然后我查了下资料,它的 ...
- 使用纯代码定义UICollectionView和自定义UICollectionViewCell
1.自定义UICollectionView 2.实现<UICollectionViewDataSource,UICollectionViewDelegateFlowLayout,UICollec ...
- 详细分享UICollectionView的自定义布局(瀑布流, 线性, 圆形…)
前言: 本篇文章不是分享collectionView的详细使用教程, 而是属于比较’高级’的collectionView使用技巧, 阅读之前, 我想你已经很熟悉collectionView的基本使用, ...
- 安卓自定义控件(四)实现自定义Layout
本来我是不准备写这篇文章的,我实在想不出有什么样奇怪的理由,会去继承ViewGroup然后自定义一个布局,大概是我的项目经验还不够吧,想了好久,想到了这样一个需求: 需求 如图:在项目中经常有一个这样 ...
随机推荐
- MVC模式学习笔记
一.如何设计一个程序的结构,这是一门专门的学问,叫做“架构模式”(architectural pattern),属于编程的方法论. MVC模式就是架构模式的一种,我觉得它不仅适用于开发软件,也适用于其 ...
- Activity的生命周期和启动模式
Activity的生命周期分析 典型情况下的生命周期.是指在用户参与的情况下,Activity所经过的生命周期的改变. 异常情况下的生命周期.是指Activity被系统回收或者由于当前设备的Confi ...
- 如何在sublime中使用sass
搞了好久,终于把sass搞定了. 最开始,我是想使用koala来实现对sass的实时编译的,但是每当我保存的时候,总是弹出erro错误,即无法编译生成css文件,百度了半天,问了好久,这个问题还是没能 ...
- HDU 2212 DFS
Problem Description A DFS(digital factorial sum) number is found by summing the factorial of every d ...
- mybatis 查询语句(按条件查询)
<select id="getAllDitch" parameterType="xxx.xx.entity.CheckDitch" resultType= ...
- jquery获取li中的各项属性值attr
发布新内容时的设计 默认显示一个按钮 如:发布按钮(放在h3字体里面)(鼠标上面时.显示发布到哪个模块下拉菜单发在li里面) $('#pup_model li , #pup_model h3').cl ...
- 3、Web应用程序中的安全向量 -- cookie盗窃
作为用户,为了防止cookie盗窃,可以在浏览器设置中选择"禁用cookie",但是这样做很可能导致在访问某个站点的时候弹出警告"该站点必须使用cookie". ...
- vs2013+opencv2.4.11+Qt5.5.1配置
注意本教程配置环境:win7 32位 如果只配置vs2013+opencv2.4.11,参考http://jingyan.baidu.com/article/ff411625b1311a12e4823 ...
- LeetCode OJ 153. Find Minimum in Rotated Sorted Array
Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 migh ...
- Openjudge-计算概论(A)-奥运奖牌计数
描述: 2008年北京奥运会,A国的运动员参与了n天的决赛项目(1≤n≤17).现在要统计一下A国所获得的金.银.铜牌数目及总奖牌数. 输入输入n+1行,第1行是A国参与决赛项目的天数n,其后n行,每 ...