Convert Figma logo to code with AI

KelvinJin logoAnimatedCollectionViewLayout

A UICollectionViewLayout subclass that adds custom transitions/animations to the UICollectionView without effecting your existing code.

4,732
349
4,732
27

Top Related Projects

:octocat: 📃 FoldingCell is an expanding content cell with animation made by @Ramotion

:octocat: ExpandingCollection is an animated material design UI card peek/pop controller. iOS library made by @Ramotion

:octocat: 🎚 RAMPaperSwitch is a Swift material design UI module which paints over the parent view when the switch is turned on. iOS library by @Ramotion

:octocat: PreviewTransition is a simple preview gallery UI controller with animated tranisitions. Swift UI library made by @Ramotion

Quick Overview

The AnimatedCollectionViewLayout is a custom UICollectionViewLayout that provides a variety of animations for cells in a UICollectionView. It offers a range of pre-built animation effects, such as fade, scale, and slide, that can be easily applied to enhance the visual appeal and user experience of your collection view.

Pros

  • Variety of Animation Effects: The library provides a wide selection of pre-built animation effects, allowing you to choose the one that best fits your app's design and user experience.
  • Easy Integration: The library is designed to be easily integrated into your existing project, with a simple and intuitive API that makes it straightforward to set up and configure the animations.
  • Customizable: The library offers a high degree of customization, allowing you to fine-tune the animation parameters to match your specific design requirements.
  • Smooth Transitions: The animations provided by the library are smooth and seamless, ensuring a polished and professional-looking user interface.

Cons

  • Limited Flexibility: While the library offers a good range of pre-built animation effects, it may not provide the level of flexibility and control that some developers might require for more complex or custom animation needs.
  • Performance Considerations: Depending on the complexity of the animations and the number of cells in the collection view, the library's performance may be a concern, especially on older or less powerful devices.
  • Dependency on UICollectionView: The library is specifically designed for UICollectionView, and may not be applicable or easily adaptable to other UI components or frameworks.
  • Potential Learning Curve: Developers who are new to the library may need to invest some time in understanding the API and how to effectively integrate the animations into their project.

Code Examples

Here are a few examples of how to use the AnimatedCollectionViewLayout in your code:

  1. Applying a Fade Animation:
let layout = AnimatedCollectionViewLayout()
layout.animator = FadeAnimator()
collectionView.collectionViewLayout = layout
  1. Applying a Scale Animation:
let layout = AnimatedCollectionViewLayout()
layout.animator = ScaleAnimator()
collectionView.collectionViewLayout = layout
  1. Applying a Slide Animation:
let layout = AnimatedCollectionViewLayout()
layout.animator = SlideAnimator()
collectionView.collectionViewLayout = layout
  1. Customizing Animation Parameters:
let layout = AnimatedCollectionViewLayout()
layout.animator = FadeAnimator()
layout.animator.duration = 0.5
layout.animator.delay = 0.1
collectionView.collectionViewLayout = layout

Getting Started

To get started with the AnimatedCollectionViewLayout, follow these steps:

  1. Add the library to your project using your preferred dependency management tool (e.g., CocoaPods, Carthage, or Swift Package Manager).

  2. Import the library in your Swift file:

import AnimatedCollectionViewLayout
  1. Create an instance of the AnimatedCollectionViewLayout and set it as the collection view's layout:
let layout = AnimatedCollectionViewLayout()
layout.animator = FadeAnimator()
collectionView.collectionViewLayout = layout
  1. (Optional) Customize the animation parameters by modifying the properties of the animator object:
layout.animator.duration = 0.5
layout.animator.delay = 0.1
  1. That's it! Your collection view should now display the selected animation effect when cells are added, removed, or reordered.

Competitor Comparisons

:octocat: 📃 FoldingCell is an expanding content cell with animation made by @Ramotion

Pros of Folding Cell

  • Folding Cell provides a visually appealing and interactive cell design, which can enhance the user experience of your app.
  • The library is well-documented and includes detailed examples, making it easy to integrate and customize.
  • Folding Cell has been actively maintained, with regular updates and bug fixes.

Cons of Folding Cell

  • Folding Cell is a specific cell design, which may not fit the overall aesthetic of your app if you're looking for a more generic animation.
  • The library may add some overhead to your app's performance, especially if you have a large number of cells.

Code Comparison

Folding Cell:

let cell = FoldingCell(style: .default, reuseIdentifier: "FoldingCell")
cell.delegate = self
cell.selectedAnimation = true
cell.isSelected = true

Animated Collection View Layout:

let layout = AnimatedCollectionViewLayout()
layout.animator = FadeAnimator()
collectionView.collectionViewLayout = layout

:octocat: ExpandingCollection is an animated material design UI card peek/pop controller. iOS library made by @Ramotion

Pros of Expanding Collection

  • Expanding Collection provides a visually appealing and interactive collection view layout, allowing users to expand and collapse cells with a smooth animation.
  • The library is well-documented and includes detailed examples, making it easy to integrate into existing projects.
  • Expanding Collection is actively maintained and has a larger community compared to AnimatedCollectionViewLayout.

Cons of Expanding Collection

  • Expanding Collection may have a higher learning curve compared to AnimatedCollectionViewLayout, as it introduces additional configuration options and custom cell handling.
  • The library may have a larger footprint in terms of file size and dependencies, which could be a concern for projects with strict size constraints.

Code Comparison

Expanding Collection:

let layout = ExpandingCollectionViewLayout()
layout.itemSize = CGSize(width: 200, height: 200)
layout.minimumLineSpacing = 16
layout.minimumInteritemSpacing = 16
collectionView.collectionViewLayout = layout

AnimatedCollectionViewLayout:

let layout = AnimatedCollectionViewLayout()
layout.itemSize = CGSize(width: 200, height: 200)
layout.minimumLineSpacing = 16
layout.minimumInteritemSpacing = 16
collectionView.collectionViewLayout = layout

As you can see, the code to set up the layout is very similar between the two libraries, with the main difference being the specific layout class used (ExpandingCollectionViewLayout vs. AnimatedCollectionViewLayout).

:octocat: 🎚 RAMPaperSwitch is a Swift material design UI module which paints over the parent view when the switch is turned on. iOS library by @Ramotion

Pros of Paper Switch

  • Paper Switch provides a visually appealing and unique switch UI element, with a paper-like animation effect.
  • The library is well-documented and easy to integrate into iOS projects.
  • Paper Switch is actively maintained and has a growing community of contributors.

Cons of Paper Switch

  • Paper Switch is limited to a specific switch UI design, whereas AnimatedCollectionViewLayout offers more flexibility in terms of collection view layout animations.
  • The library may not be suitable for projects that require a more customized or complex switch UI.
  • Paper Switch is specific to iOS, while AnimatedCollectionViewLayout can be used on both iOS and tvOS.

Code Comparison

Paper Switch:

let paperSwitch = PaperSwitch()
paperSwitch.isOn = true
paperSwitch.addTarget(self, action: #selector(switchValueChanged(_:)), for: .valueChanged)

AnimatedCollectionViewLayout:

let layout = AnimatedCollectionViewLayout()
layout.animator = FadeAnimator()
collectionView.collectionViewLayout = layout

:octocat: PreviewTransition is a simple preview gallery UI controller with animated tranisitions. Swift UI library made by @Ramotion

Pros of Ramotion/preview-transition

  • Provides a smooth and visually appealing transition between views, which can enhance the user experience.
  • Supports a variety of transition styles, including scale, fade, and slide, allowing for more customization.
  • Integrates well with UIKit, making it easy to implement in existing projects.

Cons of Ramotion/preview-transition

  • May have a higher learning curve compared to KelvinJin/AnimatedCollectionViewLayout, as it requires more configuration and setup.
  • Potentially less performant than a custom implementation, as it adds an additional layer of abstraction.

Code Comparison

KelvinJin/AnimatedCollectionViewLayout

override func layoutAttributesForItem(at indexPath: IndexPath) -> UICollectionViewLayoutAttributes? {
    guard let attributes = super.layoutAttributesForItem(at: indexPath)?.copy() as? UICollectionViewLayoutAttributes else {
        return nil
    }

    let progress = abs(attributes.center.x - collectionView!.bounds.midX) / collectionView!.bounds.width
    let scale = 1 - progress * 0.5
    attributes.transform = CGAffineTransform(scaleX: scale, y: scale)

    return attributes
}

Ramotion/preview-transition

override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
    super.viewWillTransition(to: size, with: coordinator)

    coordinator.animate(alongsideTransition: { [weak self] _ in
        self?.collectionView?.collectionViewLayout.invalidateLayout()
    }, completion: nil)
}

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

AnimatedCollectionViewLayout

Normally a UICollectionView has no transition effects when you scroll from one item to another. There are lots of ways to write animations for UICollectionView, but using a UICollectionViewLayout subclass is by far the simplest one. And it doesn't break any of your existing code. You can still use your UICollectionView subclass and you don't need to change your UICollectionViewCell. Just plug and enjoy.

CI Status

Parallax ZoomInOut RotateInOut Cards
CrossFade Cube Page

Example

To run the example project, clone the repo, and start iOS Example in Xcode.

Requirements

  • iOS 9.0+

Installation

CocoaPods

To integrate AnimatedCollectionViewLayout into your Xcode project using CocoaPods, specify it in your Podfile:

For Swift 4.2 or above:

pod 'AnimatedCollectionViewLayout'

For Swift 4.1 or below:

pod 'AnimatedCollectionViewLayout', '~> 0.3.0'

Carthage & SPM

Carthage and Swift Package Manager are also supported.

Usage

Get Started

Import the library where you want to use it. Create a AnimatedCollectionViewLayout object, set its animator and assign it to your UICollectionView.

import AnimatedCollectionViewLayout

// ...

let layout = AnimatedCollectionViewLayout()
layout.animator = ParallaxAttributesAnimator()
collectionView.collectionViewLayout = layout

Customization

Most of the built-in animators work best in Paging mode and they have additional parameters that you can tweak for better transitions. You can also write your own animators by implementing the protocol LayoutAttributesAnimator.

Author

Jin Wang

License

AnimatedCollectionViewLayout is available under the MIT license. See the LICENSE file for more info.