Skip to content

Instantly share code, notes, and snippets.

@michaelevensen
Last active April 10, 2024 08:46
Show Gist options
  • Select an option

  • Save michaelevensen/e5f756c74b8eb992836fc596056a43d1 to your computer and use it in GitHub Desktop.

Select an option

Save michaelevensen/e5f756c74b8eb992836fc596056a43d1 to your computer and use it in GitHub Desktop.

Revisions

  1. michaelevensen revised this gist Oct 21, 2016. 1 changed file with 0 additions and 2 deletions.
    2 changes: 0 additions & 2 deletions PagingCollectionViewController.swift
    Original file line number Diff line number Diff line change
    @@ -55,8 +55,6 @@ class CollectionViewController: UICollectionViewController {
    // Set delegate
    self.pagingScrollView.delegate = self

    self.pagingScrollView.backgroundColor = UIColor.red

    // Set bounds for scrollView
    self.pagingScrollView.bounds = CGRect(x: 0, y: 0, width: self.itemSize.width + self.interItemSpacing, height: collectionViewFrame.size.height)

  2. michaelevensen revised this gist Oct 20, 2016. No changes.
  3. michaelevensen revised this gist Oct 20, 2016. No changes.
  4. michaelevensen revised this gist Oct 20, 2016. 1 changed file with 4 additions and 3 deletions.
    7 changes: 4 additions & 3 deletions PagingCollectionViewController.swift
    Original file line number Diff line number Diff line change
    @@ -31,6 +31,7 @@ class CollectionViewController: UICollectionViewController {
    if scrollView == self.pagingScrollView {
    var contentOffset = scrollView.contentOffset

    // Include offset from left
    if let contentOffsetX = self.collectionView?.contentInset.left {
    contentOffset.x = contentOffset.x - contentOffsetX
    self.collectionView?.contentOffset = contentOffset
    @@ -62,7 +63,7 @@ class CollectionViewController: UICollectionViewController {
    // Number of items in UICollectionView
    if let numberOfItemsInCollectionView = self.collectionView?.numberOfItems(inSection: 0) {

    // Calculcate full width for contentSize
    // Calculcate full width (with spacing) for contentSize
    let collectionViewWidth = CGFloat(numberOfItemsInCollectionView) * (self.itemSize.width + self.interItemSpacing)

    // Set contentSize
    @@ -80,7 +81,7 @@ class CollectionViewController: UICollectionViewController {
    // Add custom paging scrollView
    self.view.addSubview(self.pagingScrollView)

    // Disable standard scrollView
    // Disable standard gesture recognizer for UICollectionView scrollView and add custom
    self.collectionView?.addGestureRecognizer(self.pagingScrollView.panGestureRecognizer)
    self.collectionView?.panGestureRecognizer.isEnabled = false
    }
    @@ -100,4 +101,4 @@ class CollectionViewController: UICollectionViewController {

    return cell
    }
    }
    }
  5. michaelevensen revised this gist Oct 20, 2016. No changes.
  6. michaelevensen revised this gist Oct 20, 2016. No changes.
  7. michaelevensen revised this gist Oct 20, 2016. 1 changed file with 0 additions and 8 deletions.
    8 changes: 0 additions & 8 deletions PagingCollectionViewController.swift
    Original file line number Diff line number Diff line change
    @@ -1,11 +1,3 @@
    //
    // CollectionViewController.swift
    // UXPagingCellCollectionView
    //
    // Created by Michael Nino Evensen on 20/10/2016.
    // Copyright © 2016 Michael Nino Evensen. All rights reserved.
    //

    import UIKit

    private let reuseIdentifier = "Cell"
  8. michaelevensen created this gist Oct 20, 2016.
    111 changes: 111 additions & 0 deletions PagingCollectionViewController.swift
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,111 @@
    //
    // CollectionViewController.swift
    // UXPagingCellCollectionView
    //
    // Created by Michael Nino Evensen on 20/10/2016.
    // Copyright © 2016 Michael Nino Evensen. All rights reserved.
    //

    import UIKit

    private let reuseIdentifier = "Cell"

    class CollectionViewController: UICollectionViewController {

    /* Custom scrollView for paging */
    let pagingScrollView = UIScrollView()

    /* Return item size */
    var itemSize: CGSize {
    let layout = self.collectionViewLayout as! UICollectionViewFlowLayout
    return layout.itemSize
    }

    /* Return spacing: Interitem */
    var interItemSpacing: CGFloat {
    let layout = self.collectionViewLayout as! UICollectionViewFlowLayout
    return layout.minimumInteritemSpacing
    }

    /* Get section inset */
    var sectionInset: UIEdgeInsets {
    let layout = self.collectionViewLayout as! UICollectionViewFlowLayout
    return layout.sectionInset
    }

    override func scrollViewDidScroll(_ scrollView: UIScrollView) {

    // Override native UICollectionView scrollView events
    if scrollView == self.pagingScrollView {
    var contentOffset = scrollView.contentOffset

    if let contentOffsetX = self.collectionView?.contentInset.left {
    contentOffset.x = contentOffset.x - contentOffsetX
    self.collectionView?.contentOffset = contentOffset
    }
    }
    }

    // MARK: - Initialize scrollView with proper size and frame
    func initScrollView() {

    // Get UICollectionView frame
    if let collectionViewFrame = self.collectionView?.frame {

    // Set proper frame
    self.pagingScrollView.frame = collectionViewFrame
    self.pagingScrollView.isHidden = true

    // Enable paging
    self.pagingScrollView.isPagingEnabled = true

    // Set delegate
    self.pagingScrollView.delegate = self

    self.pagingScrollView.backgroundColor = UIColor.red

    // Set bounds for scrollView
    self.pagingScrollView.bounds = CGRect(x: 0, y: 0, width: self.itemSize.width + self.interItemSpacing, height: collectionViewFrame.size.height)

    // Number of items in UICollectionView
    if let numberOfItemsInCollectionView = self.collectionView?.numberOfItems(inSection: 0) {

    // Calculcate full width for contentSize
    let collectionViewWidth = CGFloat(numberOfItemsInCollectionView) * (self.itemSize.width + self.interItemSpacing)

    // Set contentSize
    self.pagingScrollView.contentSize = CGSize(width: collectionViewWidth, height: view.frame.size.height)
    }
    }
    }

    override func viewDidLoad() {
    super.viewDidLoad()

    // Initialize Paging scrollView
    self.initScrollView()

    // Add custom paging scrollView
    self.view.addSubview(self.pagingScrollView)

    // Disable standard scrollView
    self.collectionView?.addGestureRecognizer(self.pagingScrollView.panGestureRecognizer)
    self.collectionView?.panGestureRecognizer.isEnabled = false
    }


    // MARK: UICollectionViewDataSource
    override func numberOfSections(in collectionView: UICollectionView) -> Int {
    return 1
    }

    override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return 10
    }

    override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath)

    return cell
    }
    }