expanding-collection
环境需求
iOS 9.0+
Xcode 9.0+
安装方式
只需将Source文件夹添加到您的项目.
或者使用 CocoaPods ,Podfile 文件中添加以下一行:
pod 'expanding-collection'
or Carthage Cartfile
文件添加以下一行:
github "Ramotion/expanding-collection"
使用方法
import expanding_collection
创建 CollectionViewCell
- 创建UICollectionViewCell继承自BasePageCollectionCell(建议使用xib文件创建cell)
- 添加前视图FrontView
添加一个视图到您的单元格。连接到
@IBOutlet weak var frontContainerView: UIView!
添加宽度、高度、centerX和centerY约束(宽度和高度约束必须等于单元格大小)
将centerY约束连接到
@IBOutlet weak var frontConstraintY: NSLayoutConstraint!
添加任何想要的UIView到frontView
- 添加BackView
重复步骤2(将outlet连接到
@IBOutlet weak var backContainerView: UIView!,
@IBOutlet weak var backConstraintY: NSLayoutConstraint!
)
- Cell example DemoCell
如果对任何FrontView设置tag = 101。此视图将在转换动画期间隐藏
创建 CollectionViewController
- 创建一个继承自ExpandingViewController的UIViewController
- 注册单元格和设置单元格大小:
override func viewDidLoad() {
itemSize = CGSize(width: 214, height: 264)
super.viewDidLoad()
// register cell
let nib = UINib(nibName: "NibName", bundle: nil)
collectionView?.registerNib(nib, forCellWithReuseIdentifier: "CellIdentifier")
}
- 添加UICollectionViewDataSource方法
extension YourViewController {
override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return items.count
}
override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("CellIdentifier"), forIndexPath: indexPath)
// configure cell
return cell
}
}
- 打开Cell动画
override func viewDidLoad() {
itemSize = CGSize(width: 214, height: 264)
super.viewDidLoad()
// register cell
let nib = UINib(nibName: "CellIdentifier", bundle: nil)
collectionView?.registerNib(nib, forCellWithReuseIdentifier: String(DemoCollectionViewCell))
}
func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
cell.cellIsOpen(!cell.isOpened)
}
如果使用委托方法:
func collectionView(collectionView: UICollectionView, willDisplayCell cell: UICollectionViewCell, forItemAtIndexPath indexPath: NSIndexPath)
func scrollViewDidEndDecelerating(scrollView: UIScrollView)
必须调用超方法:
func collectionView(collectionView: UICollectionView, willDisplayCell cell: UICollectionViewCell, forItemAtIndexPath indexPath: NSIndexPath) {
super.collectionView(collectionView: collectionView, willDisplayCell cell: cell, forItemAtIndexPath indexPath: indexPath)
// code
}
func scrollViewDidEndDecelerating(scrollView: UIScrollView) {
super.scrollViewDidEndDecelerating(scrollView: scrollView)
// code
}
过渡动画
- 创建一个继承自ExpandingTableViewController的UITableViewController
- 设置标题高度默认236
override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) {
super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
headerHeight = ***
}
或者
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
headerHeight = ***
}
- 调用viewcontroller中的push方法到tableviewcontroller
if cell.isOpened == true {
let vc: YourTableViewController = // ... create view controller
pushToViewController(vc)
}
- 对于反向转换,使用popTransitionAnimation()