欢迎光临
我们一直在努力

[Swift2]Display Switcher

建站超值云服务器,限时71元/月

[Swift2]Display Switcher

项目介绍:

[分享]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 直接下载:

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » [Swift2]Display Switcher
分享到: 更多 (0)