Swift-UICollectionView布局之线性布局
来源:互联网 发布:淘宝店铺介绍 编辑:程序博客网 时间:2024/06/05 07:28
Swift-UICollectionView布局之线性布局
应用场景
- 轮播图(AppStore)
- 数据展示(招商银行账单页)
- 图片查看器
实现思路
- 线性布局,在二维平面上滚动,所以继承自流水布局(
UICollectionViewFlowLayout
) - 流水布局提供一下属性:
- itemSize
- sectionInset
- scrollDirection
- minimumLineSpacing
- 每个cell都对应一个布局属性(
UICollectionViewLayoutAttributes
),布局属性决定cell怎么排布,我们只需要修改布局属性,就可以随意设置cell显示效果了
实现步骤
创建线性布局类LinerLayout
// 继承自UICollectionViewFlowLayoutpublic class LinerLayout: UICollectionViewFlowLayout {}
私有属性
// 常量private struct InnerConstant { static let MinScaleW: CGFloat = 0.8 static let MinScaleH: CGFloat = 0.3 static let MinAlpha: CGFloat = 0 static let SetAlpha = true}
公开属性
// width最小的缩放比public var minScaleW = InnerConstant.MinScaleW// height最小的缩放比public var minScaleH = InnerConstant.MinScaleH// 是否需要设置alphapublic var setAlpha = InnerConstant.SetAlpha// alpha 最小的缩放比public var minAlpha = InnerConstant.MinAlpha
重写父类方法
prepareLayout
/// 准备操作 设置一些初始化参数override public func prepareLayout() { let inset = (collectionView!.frame.size.width - itemSize.width) * 0.5 sectionInset = UIEdgeInsetsMake(0, inset, 0, inset) // 要后调用super.prepareLayout()方法,否则有很多警告…… // http://stackoverflow.com/questions/32082726/the-behavior-of-the-uicollectionviewflowlayout-is-not-defined-because-the-cell super.prepareLayout()}
layoutAttributesForElementsInRect
////// 返回collectionView上面当前显示的所有元素(比如cell)的布局属性:这个方法决定了cell怎么排布/// 每个cell都有自己对应的布局属性:UICollectionViewLayoutAttributes/// 要求返回的数组中装着UICollectionViewLayoutAttributes对象///override public func layoutAttributesForElementsInRect(rect: CGRect) -> [UICollectionViewLayoutAttributes]? { // 屏幕上显示的cell let array = super.layoutAttributesForElementsInRect(rect) ?? [] // This is likely occurring because the flow layout subclass GalaryManager.LinerLayout is modifying attributes returned by UICollectionViewFlowLayout without copying them // http://stackoverflow.com/questions/32720358/xcode-7-copy-layoutattributes var attributesCopy = [UICollectionViewLayoutAttributes]() for itemAttributes in array { let itemAttributesCopy = itemAttributes.copy() as! UICollectionViewLayoutAttributes // add the changes to the itemAttributesCopy attributesCopy.append(itemAttributesCopy) } // 计算 CollectionView 的中点 let centerX = collectionView!.contentOffset.x + collectionView!.frame.size.width * 0.5 for attrs in attributesCopy { // 计算 cell 中点的 x 值 与 centerX 的差值 let delta = abs(centerX - attrs.center.x) // W:[0.8 ~ 1.0] // H:[0.3 ~ 1.0] // 反比 let baseScale = 1 - delta / (collectionView!.frame.size.width + itemSize.width) let scaleW = minScaleW + baseScale * (1 - minScaleW) let scaleH = minScaleH + baseScale * (1 - minScaleH) let alpha = minAlpha + baseScale * (1 - minAlpha) // 改变transform(越到中间 越大) attrs.transform =CGAffineTransformMakeScale(scaleW, scaleH) if setAlpha { // 改变透明度(越到中间 越不透明) attrs.alpha = abs(alpha) } } return attributesCopy}
shouldInvalidateLayoutForBoundsChange
/// 当collectionView的bounds发生改变时,是否要刷新布局////// 一定要调用这个方法override public func shouldInvalidateLayoutForBoundsChange(newBounds: CGRect) -> Bool { return true}
targetContentOffsetForProposedContentOffset
/// targetContentOffset :通过修改后,collectionView最终的contentOffset(取决定情况)/// proposedContentOffset :默认情况下,collectionView最终的contentOffsetoverride public func targetContentOffsetForProposedContentOffset(proposedContentOffset: CGPoint, withScrollingVelocity velocity: CGPoint) -> CGPoint { let size = collectionView!.frame.size // 计算可见区域的面积 let rect = CGRectMake(proposedContentOffset.x, proposedContentOffset.y, size.width, size.height) let array = super.layoutAttributesForElementsInRect(rect) ?? [] // 计算 CollectionView 中点值 let centerX = proposedContentOffset.x + collectionView!.frame.size.width * 0.5 // 标记 cell 的中点与 UICollectionView 中点最小的间距 var minDetal = CGFloat(MAXFLOAT) for attrs in array { if abs(minDetal) > abs(centerX - attrs.center.x) { minDetal = attrs.center.x - centerX } } return CGPointMake(proposedContentOffset.x + minDetal, proposedContentOffset.y)}
遇到的坑
因为是从OC版本迁移过来的,只是语法不同!但运行的时候,出现了若干警告!解决办法如下:
http://stackoverflow.com/questions/32082726/the-behavior-of-the-uicollectionviewflowlayout-is-not-defined-because-the-cell
http://stackoverflow.com/questions/32720358/xcode-7-copy-layoutattributes
使用示例
override func viewWillAppear(animated: Bool) { super.viewWillAppear(animated) // 设置布局属性 // 因为对于不同的需求, itemSize、scrollDirection和minimumLineSpacing的值可能不同,所以没有封装到LinerLayout里面,调用方需要手动设置 let width = collectionView!.frame.size.width * 0.69 let height = collectionView!.frame.size.height * 0.71 layout.itemSize = CGSizeMake(width, height) layout.scrollDirection = .Horizontal layout.minimumLineSpacing = 10}
0 0
- Swift-UICollectionView布局之线性布局
- 自定义UICollectionView布局-线性布局
- UICollectionView之自定义布局
- UICollectionView详解之自定义布局
- iOS之UICollectionView自定义布局
- iOS之UICollectionView自定义布局
- android布局之线性布局
- android布局之线性布局
- Android布局之线性布局
- Android布局之线性布局
- android布局之线性布局
- 布局简介之线性布局
- Layout布局之线性布局
- Android布局之线性布局
- Android之线性布局
- iOS - UICollectionView 自定义布局之风火轮[译] (原版为swift,我这里的实现改为OC)
- 【iOS】UICollectionView自定义Layout之蜂窝布局
- UICollectionView自定义布局之风火轮[译]
- Linux gpio 按键中断学习
- java基础知识点
- 图片的底片效果、老照片效果、浮雕效果、黑白色效果
- 高性能服务器架构思路【不仅是思路】
- JDK1.8-新特性
- Swift-UICollectionView布局之线性布局
- poj2376 贪心算法
- C#——窗体实验—进阶-文本框实验
- Drupal 自定义模块条码生成
- Shader and Program编程基本概念 - 转
- 162. Find Peak Element
- 华为--嵌入式软件工程师面试题汇总
- php 生成条形码(基于yii)
- Xen虚拟化技术的模式和演变