Last active
April 10, 2024 08:46
-
-
Save michaelevensen/e5f756c74b8eb992836fc596056a43d1 to your computer and use it in GitHub Desktop.
Revisions
-
michaelevensen revised this gist
Oct 21, 2016 . 1 changed file with 0 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -55,8 +55,6 @@ class CollectionViewController: UICollectionViewController { // Set delegate self.pagingScrollView.delegate = self // Set bounds for scrollView self.pagingScrollView.bounds = CGRect(x: 0, y: 0, width: self.itemSize.width + self.interItemSpacing, height: collectionViewFrame.size.height) -
michaelevensen revised this gist
Oct 20, 2016 . No changes.There are no files selected for viewing
-
michaelevensen revised this gist
Oct 20, 2016 . No changes.There are no files selected for viewing
-
michaelevensen revised this gist
Oct 20, 2016 . 1 changed file with 4 additions and 3 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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 (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 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 } } -
michaelevensen revised this gist
Oct 20, 2016 . No changes.There are no files selected for viewing
-
michaelevensen revised this gist
Oct 20, 2016 . No changes.There are no files selected for viewing
-
michaelevensen revised this gist
Oct 20, 2016 . 1 changed file with 0 additions and 8 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,11 +1,3 @@ import UIKit private let reuseIdentifier = "Cell" -
michaelevensen created this gist
Oct 20, 2016 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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 } }