[个人博客搬运]定制UICollectionViewLayout

2017-03-23  本文已影响116人  zzqiltw

最新的需求中,需要实现一个展示宝贝上新的视图,展现方式是点击一个UITableView的cell,在cell拉出一个空白的“抽屉”视图,“抽屉”中横向展示可滚动的多个宝贝的图文视图,于是很自然地想到用UICollectionView来实现。后来这个需求被砍掉了(orz),不过UICollectionView是一个很有意思很灵活的视图,类似Android的GridView,但是比之功能更强大,它可以具体到每一个item的定制,完全取决于UICollectionViewLayout的实现。

answer huang的译文中这样说明UICollectionView:

UITableView和UICollectionView都是由data-source和delegate驱动的。他们为其显示的子视图集扮演为愚蠢的容器(dumb containers),对他们真实的内容(contents)毫不知情。

UICollectionView进一步抽象了。它将其子视图的位置,大小和外观的控制权委托给一个单独的布局对象。通过提供一个自定义布局对象,你几乎可以实现任何你能想象到的布局。布局继承自UICollectionViewLayout这个抽象基类。iOS6中以UICollectionViewFlowLayout类的形式提出了一个具体的布局实现。

关于UICollectionView,其他的同事已经介绍过啦,可以看这里

我们写一个简单的小demo,效果类似这样
UICollectionViewFlowLayout

这是最简单的流水布局:UICollectionViewFlowLayout。类如其义,它提供一个流水布局,item(类似UITableView中的cell)会排列在上一个item的右边,如果屏幕空间不够,它会自动排到下一行,和水流一样。

代码如下:

我们现在想实现一个这样的效果:
当左右拖动item的时候,靠近屏幕中心item放大,远离中心的item缩小
左右滑动的时候,停止下来的item的中心永远和屏幕中心对齐

效果如图:
linear

首先,我们需要自定义UICollectionViewLayout。由于需求效果仍然保持流水特性,我们选择继承UICollectionViewFlowLayout。需要重写的方法如下:

代码如下:

以上是继承UICollectionViewFlowLayout的布局,如果我们想做一次更深层次的定制,我们可以直接继承UICollectionViewLayout来完全自定义布局,要重写的方法和UICollectionViewFlowLayout类似,只是我们需要自己来实现这些方法:

代码如下:

}
以上可实现一个环形(circle)布局, demo中还添加了点击item就把该item删除的事件
circel

还可以做一个类似蜂巢的布局:

-(NSArray *)layoutAttributesForElementsInRect:(CGRect)rect
{
NSArray *arr = [super layoutAttributesForElementsInRect:rect];
if ([arr count] > 0) {
return arr;
}
NSMutableArray *attributes = [NSMutableArray array];
for (NSInteger i = 0 ; i < [self.collectionView numberOfItemsInSection:0 ]; i++) {
NSIndexPath *indexPath = [NSIndexPath indexPathForItem:i inSection:0];
[attributes addObject:[self layoutAttributesForItemAtIndexPath:indexPath]];
}
return attributes;
}

其中cell的样式:

{

if (self = [super initWithFrame:frame]) {

    self.backgroundColor = ZQRandomColor;

    CGFloat longSide = SIZE * 0.5 * cosf(M_PI * 30 / 180);

    CGFloat shortSide = SIZE * 0.5 * sinf(M_PI * 30 / 180);

    UIBezierPath *path = [UIBezierPath bezierPath];

    [path moveToPoint:CGPointMake(0, longSide)];

    [path addLineToPoint:CGPointMake(shortSide, 0)];

    [path addLineToPoint:CGPointMake(shortSide + SIZE * 0.5, 0)];

    [path addLineToPoint:CGPointMake(SIZE, longSide)];

    [path addLineToPoint:CGPointMake(shortSide + SIZE * 0.5, longSide * 2)];

    [path addLineToPoint:CGPointMake(shortSide, longSide * 2)];

    [path closePath];

    

    CAShapeLayer *maskLayer = [CAShapeLayer layer];

    maskLayer.path = [path CGPath];

    

    self.layer.mask = maskLayer;

}

return self;

}

效果如图:
honeycomb

上一篇 下一篇

猜你喜欢

热点阅读