expanding-collection
環(huán)境需求
iOS 9.0+
Xcode 9.0+
安裝方式
只需將Source文件夾添加到您的項(xiàng)目.
或者使用 CocoaPods ,Podfile 文件中添加以下一行:
pod 'expanding-collection'
or Carthage Cartfile
文件添加以下一行:
github "Ramotion/expanding-collection"
使用方法
import expanding_collection
創(chuàng)建 CollectionViewCell
- 創(chuàng)建UICollectionViewCell繼承自BasePageCollectionCell(建議使用xib文件創(chuàng)建cell)
- 添加前視圖FrontView
添加一個(gè)視圖到您的單元格置森。連接到
@IBOutlet weak var frontContainerView: UIView!
添加寬度、高度鳖枕、centerX和centerY約束(寬度和高度約束必須等于單元格大小)
將centerY約束連接到
@IBOutlet weak var frontConstraintY: NSLayoutConstraint!
添加任何想要的UIView到frontView
- 添加BackView
重復(fù)步驟2(將outlet連接到
@IBOutlet weak var backContainerView: UIView!,
@IBOutlet weak var backConstraintY: NSLayoutConstraint!
)
- Cell example DemoCell
如果對(duì)任何FrontView設(shè)置tag = 101。此視圖將在轉(zhuǎn)換動(dòng)畫期間隱藏
創(chuàng)建 CollectionViewController
- 創(chuàng)建一個(gè)繼承自ExpandingViewController的UIViewController
- 注冊(cè)單元格和設(shè)置單元格大小:
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動(dòng)畫
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)
必須調(diào)用超方法:
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
}
過渡動(dòng)畫
- 創(chuàng)建一個(gè)繼承自ExpandingTableViewController的UITableViewController
- 設(shè)置標(biāo)題高度默認(rèn)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 = ***
}
- 調(diào)用viewcontroller中的push方法到tableviewcontroller
if cell.isOpened == true {
let vc: YourTableViewController = // ... create view controller
pushToViewController(vc)
}
- 對(duì)于反向轉(zhuǎn)換,使用popTransitionAnimation()