项目介绍:
[分享]Display Switcher
Display Switcher实现了在两个collection view layouts之间的自定义转场动画
Requirements 环境需求
- iOS 8.0+ (for component), iOS 9.0+ (for example)
- Xcode 7
- Swift 2
Installing with CocoaPods
use_frameworks!
pod ‘DisplaySwitcher’, ‘~> 0.1.1’
Installing with Carthage
github "Yalantis/DisplaySwitcher" ~> 0.1.0
使用方法:
import DisplaySwitcher:
[Objective-C] 查看源文件 复制代码
import DisplaySwitcher
Then create two layouts (layout for list mode and grid mode): 创建两种
layouts
[Objective-C] 查看源文件 复制代码
private lazy var listLayout = BaseLayout(staticCellHeight: listLayoutStaticCellHeight, nextLayoutStaticCellHeight: gridLayoutStaticCellHeight, layoutState: .ListLayoutState) private lazy var gridLayout = BaseLayout(staticCellHeight: gridLayoutStaticCellHeight, nextLayoutStaticCellHeight: listLayoutStaticCellHeight, layoutState: .GridLayoutState)
Set current layout:
[Objective-C] 查看源文件 复制代码
private var layoutState: CollectionViewLayoutState = .ListLayoutState collectionView.collectionViewLayout = listLayout
重写UICollectionViewDataSource方法
[Objective-C] 查看源文件 复制代码
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { // count of items } func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell { // configure your custom cell }
重写UICollectionViewDelegate方法用来自定义 layout transition
[Objective-C] 查看源文件 复制代码
func collectionView(collectionView: UICollectionView, transitionLayoutForOldLayout fromLayout: UICollectionViewLayout, newLayout toLayout: UICollectionViewLayout) -> UICollectionViewTransitionLayout { let customTransitionLayout = TransitionLayout(currentLayout: fromLayout, nextLayout: toLayout) return customTransitionLayout }
以下方法创建转场动画(可以更改动画时间和汉堡按钮的旋转角度度数)
[Objective-C] 查看源文件 复制代码
let transitionManager: TransitionManager if layoutState == .ListLayoutState { layoutState = .GridLayoutState transitionManager = TransitionManager(duration: animationDuration, collectionView: collectionView!, destinationLayout: gridLayout, layoutState: layoutState) } else { layoutState = .ListLayoutState transitionManager = TransitionManager(duration: animationDuration, collectionView: collectionView!, destinationLayout: listLayout, layoutState: layoutState) } transitionManager.startInteractiveTransition() rotationButton.selected = layoutState == .ListLayoutState rotationButton.animationDuration = animationDuration
DEMO 直接下载: