Skip to content

Custom transition between two collection view layouts

License

Notifications You must be signed in to change notification settings

mehmetbaykar/DisplaySwitcher

 
 

Repository files navigation

Display Switcher

CocoaPods Compatible Platform License Yalantis

This component implements custom transition between two collection view layouts with custom animation duration.

Preview

Check this project on dribbble.

Also, read how it was done in our blog

Requirements

  • iOS 8.0+
  • Xcode 9
  • Swift 4.2

Installing

use_frameworks!
pod ‘DisplaySwitcher’, '~> 1.2
github "Yalantis/DisplaySwitcher" "master"

Usage

At first, import DisplaySwitcher:

import DisplaySwitcher

Then create two layouts (list mode and grid mode):

private lazy var listLayout = DisplaySwitchLayout(staticCellHeight: listLayoutStaticCellHeight, nextLayoutStaticCellHeight: gridLayoutStaticCellHeight, layoutState: .list)

private lazy var gridLayout = DisplaySwitchLayout(staticCellHeight: gridLayoutStaticCellHeight, nextLayoutStaticCellHeight: listLayoutStaticCellHeight, layoutState: .grid)

Set current layout:

private var layoutState: LayoutState = .list
collectionView.collectionViewLayout = listLayout

Then override two UICollectionViewDataSource methods:

func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    // count of items
}

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
    // configure your custom cell
}

Also override one UICollectionViewDelegate method (for custom layout transition):

func collectionView(collectionView: UICollectionView, transitionLayoutForOldLayout fromLayout: UICollectionViewLayout, newLayout toLayout: UICollectionViewLayout) -> UICollectionViewTransitionLayout {
    let customTransitionLayout = TransitionLayout(currentLayout: fromLayout, nextLayout: toLayout)
    return customTransitionLayout
}

And in the end necessary create transition and start it (you can simply change animation duration for transition layout and for rotation button):

let transitionManager: TransitionManager
if layoutState == .list {
    layoutState = .grid
    transitionManager = TransitionManager(duration: animationDuration, collectionView: collectionView!, destinationLayout: gridLayout, layoutState: layoutState)
} else {
    layoutState = .list
    transitionManager = TransitionManager(duration: animationDuration, collectionView: collectionView!, destinationLayout: listLayout, layoutState: layoutState)
}
transitionManager.startInteractiveTransition()
rotationButton.selected = layoutState == .list
rotationButton.animationDuration = animationDuration

Have fun! :)

Let us know!

We’d be really happy if you sent us links to your projects where you use our component. Just send an email to github@yalantis.com And do let us know if you have any questions or suggestion regarding the animation.

P.S. We’re going to publish more awesomeness wrapped in code and a tutorial on how to make UI for iOS (Android) better than better. Stay tuned!

License

The MIT License (MIT)

Copyright © 2018 Yalantis

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

About

Custom transition between two collection view layouts

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Swift 96.2%
  • Ruby 2.7%
  • Other 1.1%