16

GitHub - amirdew/CollectionViewPagingLayout: Simple layout for make paging with...

 4 years ago
source link: https://github.com/amirdew/CollectionViewPagingLayout
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

README.md

CollectionViewPagingLayout

Version License Platform

Preview

68747470733a2f2f616d69722e6170702f6769742f666c6f776c61796f75745f707265766965772e676966 68747470733a2f2f616d69722e6170702f6769742f67616c6c6572795f707265766965772e676966

68747470733a2f2f616d69722e6170702f6769742f636172645f737461636b5f707265766965772e676966

About

A custom UICollectionViewLayout that gives you the ability to apply transforms easily on the cells
by conforming your cell class to TransformableView protocol you will get a progress value and you can use it to change the cell view.
You can see the usage by looking into the example source code.
More examples will be added.

Add to your project

Cocoapods

CollectionViewPagingLayout is available through CocoaPods. To install it, simply add the following line to your Podfile:

pod "CollectionViewPagingLayout"

Manually

Just add all the files under Lib directory to your project

How to use

  • make sure you imported the library if you use cocoapods
import CollectionViewPagingLayout
  • Set the layout for collection view: (in most cases you want a paging effect so make sure you enable it)
let layout = CollectionViewPagingLayout()
collectionView.collectionViewLayout = layout
collectionView.isPagingEnabled = true // enabling paging effect
  • Here you can set numberOfVisibleItems, by default it's null and that means it will load all of the cells at a time
layout.numberOfVisibleItems = ...
  • Now you just need to conform your UICollectionViewCell class to TransformableView and start implementing your custom transforms

progress is a float value that represents current position of the cell.
When it's 0 means the current position of the cell is exactly in the center of your collection view.
the value could be negative or positive and that represents the distance to the center of your collection view.
for instance 1 means the distance between the center of the cell and the center of your collection view is equal to your collection view width.

extension MyCollectionViewCell: TransformableView {
    func transform(progress: CGFloat) {
      ...
    }
}

you can start with a simple transform like this:

extension MyCollectionViewCell: TransformableView {
    func transform(progress: CGFloat) {
        let transform = CGAffineTransform(translationX: bounds.width/2 * progress, y: 0)
        let alpha = 1 - abs(progress)

        contentView.subviews.forEach { $0.transform = transform }
        contentView.alpha = alpha
    }
}

Limitations

You need to specify the number of visible cells since this layout gives you the flexibility to show the next and previous cells.
By default, the layout loads all of the cells in the collection view frame and that means it keeps all of them in memory. You can specify the number of cells that you need to show at a time by considering your design.

License

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


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK