Giter Site home page Giter Site logo

milligator / centeredcollectionview Goto Github PK

View Code? Open in Web Editor NEW

This project forked from benemdon/centeredcollectionview

0.0 1.0 0.0 3.75 MB

A lightweight UICollectionViewLayout that 'pages' and centers it's cells ๐ŸŽก written in Swift

License: MIT License

Ruby 14.24% Swift 44.61% Objective-C 3.37% Shell 37.78%

centeredcollectionview's Introduction

CenteredCollectionView

CI Status Version Platform Carthage compatible Swift 3 License

CenteredCollectionView is a lightweight drop in place UICollectionViewFlowLayout that pages and keeps its cells centered, resulting in the "carousel effect" ๐ŸŽก

Example ๐Ÿ“ฑ

Demo

To try the example using Cocoapods:

pod try CenteredCollectionView

Usage ๐Ÿ› 

let centeredCollectionViewFlowLayout = CenteredCollectionViewFlowLayout()
let collectionView: UICollectionView

override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) {
  collectionView = UICollectionView(centeredCollectionViewFlowLayout: centeredCollectionViewFlowLayout)
  super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
}

...

override func viewDidLoad() {
  super.viewDidLoad()

  // implement the delegate and dataSource
  collectionView.delegate = self
  collectionView.dataSource = self

  // layout subviews (not shown)
  ...

  // register collection cells
  collectionView.register(UICollectionViewCell.self, forCellWithReuseIdentifier: String(describing: UICollectionViewCell.self))

  // configure CenteredCollectionViewFlowLayout properties
  centeredCollectionViewFlowLayout.itemSize = CGSize(width: 100, height: 100)
  centeredCollectionViewFlowLayout.minimumLineSpacing = 20

  // get rid of scrolling indicators
  collectionView.showsVerticalScrollIndicator = false
  collectionView.showsHorizontalScrollIndicator = false
}

// delegate and datasource extensions
...

Scrolling to an Edge on Touch ๐ŸŽก

scrollToEdgeEnabled

Heres how you could trigger a scroll animation when a touch happens on an item that isn't the currentCenteredPage:

extension ViewController: UICollectionViewDelegate {
  func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {

    // check if the currentCenteredPage is not the page that was touched
    let currentCenteredPage = centeredCollectionViewFlowLayout.currentCenteredPage,
    			currentCenteredPage != indexPath.row {
      // trigger a scrollTo(index: animated:)
      centeredCollectionView.scrollTo(index: indexPath.row, animated: true)
    }
  }
}

Customize ๐Ÿ–Œ

You can use all properties inherited from UICollectionView.

CenteredCollectionViewFlowLayout specific properties:

  • minimumLineSpacing amount of space between each cell

    var minimumLineSpacing: CGFloat { get set }
    // default: 10
  • itemSize size of each cell. โš ๏ธ required for use

    var itemSize: CGSize { get set }
  • currentCenteredPage calculates the current centered page

    var currentCenteredPage: Int? { get }
  • scrollDirection direction of scrolling (supports vertical)

    var scrollDirection: UICollectionViewScrollDirection { get set }
    // default: .horizontal
  • scrollTo(index: animated:) programatically scrolls to a cell at a specified index.

    func scrollTo(index: Int, animated: Bool)

Requirements โœ…

This pod requires a deployment target of iOS 9.0 or greater

Installation ๐Ÿ“ฒ

CenteredCollectionView is available through CocoaPods and Carthage.

To install it with Cocoapods, add the following line to your Podfile:

pod "CenteredCollectionView"

To install it with Carthage, add the following line to your Cartfile:

github "BenEmdon/CenteredCollectionView"

Contributing ๐Ÿ’ก

All contributions are welcome! If you make a pull request or an issue, you're likely to get a swift response!

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

@BenEmdon, [email protected]

License ๐Ÿ“„

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

centeredcollectionview's People

Contributors

benemdon avatar martin-key avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.