Skip to content

UICollectionViewFlowLayout with behaviour to snap the cell when scrolling.

License

Notifications You must be signed in to change notification settings

getmimo/SnappingLayout

Repository files navigation

SnappingLayout

Version License Platform swift-version Carthage compatible swift-package-manager

๐ŸŒŽ Description

SnappingLayout enables a snap behaviour for UICollectionViews. It works with horizontal scrolling and there are 3 different types: left, center and right. You can scroll your collection view and it will automatically snap to the chosen position after decelerating.

The snap supports all kinds of collection view configurations: with sectionInset, minimumLineSpacing, contentInset and so forth.

โ˜• Contents

๐Ÿšฅ Requirements

  • iOS 9.0+
  • Swift 4.2+

๐Ÿ’ป Installation

CocoaPods

CocoaPods is a dependency manager for Cocoa projects. You can install it with the following command:

$ gem install cocoapods

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

pod 'SnappingLayout'

Then, run the following command:

$ pod install

Carthage

only available from version 0.1.2 onwards

Carthage is a decentralized dependency manager that builds your dependencies and provides you with binary frameworks.

You can install Carthage with Homebrew using the following command:

$ brew update
$ brew install carthage

To integrate SnappingLayout into your Xcode project using Carthage, specify it in your Cartfile:

github "SnappingLayout" ~> 0.1.2

Run carthage update to build the framework and drag the built SnappingLayout.framework into your Xcode project.

Swift Package Manager

only available from version 0.1.4 onwards

Swift Package Manager is a tool for managing the distribution of Swift code. Itโ€™s integrated with the Swift build system to automate the process of downloading, compiling, and linking dependencies.

To integrate SnappingLayout into your Xcode project using Swift Package Manager, add it to the dependencies value of your Package.swift:

dependencies: [
    .package(url: "https://github.com/getmimo/SnappingLayout.git", .upToNextMajor(from: "0.1.4"))
]

Manually

If you prefer not to use either of the aforementioned dependency managers, you can always rely on the good old way of integrating SnappingLayout into your project manually.

๐ŸŽ“ Example Project

To run the example project, clone the repo, and run pod install from the Example directory first. Open SnappingPresentationViewController to see the code needed to implement the snapping.

๐Ÿ‚ Usage

Just use SnappingLayout as a replacement for the traditional UICollectionViewFlowLayout.

It's as simple as:

  1. Importing it:
import SnappingLayout
  1. Instantiating it:
let snappingLayout = SnappingLayout()
  1. And using it:
let collectionView = UICollectionView(frame: .zero, collectionViewLayout: snappingLayout)
  1. Set the deceleration rate of the collection view to fast:
collectionView.decelerationRate = .fast

There are three types of snappingLayout: left, center and right.

Left

This is the default value when a new instance of snappingLayout is created and it will snap the position of the cell to the left side of the collection view.

let snappingLayout = SnappingLayout()
snappingLayout.snapPosition = .left

left

Center

This will snap the position of the cell to the center of the collection view.

let snappingLayout = SnappingLayout()
snappingLayout.snapPosition = .center

center

Right

This will snap the position of the cell to the right of the collection view.

let snappingLayout = SnappingLayout()
snappingLayout.snapPosition = .right

right

๐Ÿ’ฌ Contributing

This is an open source project, so feel free to contribute. How?

  • Open an issue;
  • Send feedback via email;
  • Propose your own fixes, suggestions and open a pull request with the changes.

See all contributors.

๐Ÿ‘จโ€๐Ÿ’ป Authors

๐Ÿ‘ฎ License

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