From 037a932409cc2ad1c522e8dca5b471691cebcd2f Mon Sep 17 00:00:00 2001 From: hryk224 Date: Thu, 17 Nov 2016 14:43:16 +0900 Subject: [PATCH] Removed unneeded property --- .gitignore | 3 +- Example/Example.xcodeproj/project.pbxproj | 44 +++++++++++------ Example/Example/ImageCollectionViewCell.swift | 2 +- Example/Example/MainViewController.swift | 22 ++++++--- Example/Example/Pattern1.storyboard | 4 +- Example/Example/Pattern1ViewController.swift | 10 ++-- Example/Example/Pattern2.storyboard | 34 +++++++------- Example/Example/Pattern2ViewController.swift | 47 +++++++++---------- InfiniteCollectionView.podspec | 2 +- .../InfiniteCollectionView.swift | 21 ++++----- README.md | 2 +- 11 files changed, 105 insertions(+), 86 deletions(-) diff --git a/.gitignore b/.gitignore index 0104343..cf6098d 100644 --- a/.gitignore +++ b/.gitignore @@ -20,4 +20,5 @@ DerivedData *.ipa *.app -*.xccheckout \ No newline at end of file +*.xccheckout +sort-Xcode-project-file \ No newline at end of file diff --git a/Example/Example.xcodeproj/project.pbxproj b/Example/Example.xcodeproj/project.pbxproj index 2570874..8b3c139 100644 --- a/Example/Example.xcodeproj/project.pbxproj +++ b/Example/Example.xcodeproj/project.pbxproj @@ -86,8 +86,8 @@ isa = PBXGroup; children = ( 935BD6C71CE26EB600E5FDF7 /* Example */, - 935BD6C61CE26EB600E5FDF7 /* Products */, EDA05AD11D92901900F7C2C5 /* Frameworks */, + 935BD6C61CE26EB600E5FDF7 /* Products */, ); sourceTree = ""; }; @@ -102,22 +102,38 @@ 935BD6C71CE26EB600E5FDF7 /* Example */ = { isa = PBXGroup; children = ( + ED510E6D1DDD513200470C8B /* Pattern1 */, + ED510E6E1DDD513F00470C8B /* Pattern2 */, 935BD6C81CE26EB600E5FDF7 /* AppDelegate.swift */, + 935BD6CF1CE26EB600E5FDF7 /* Assets.xcassets */, 935BD6DA1CE26F0B00E5FDF7 /* ImageCollectionViewCell.swift */, 935BD6DB1CE26F0B00E5FDF7 /* ImageCollectionViewCell.xib */, - 935BD6DC1CE26F0B00E5FDF7 /* MainViewController.swift */, - 935BD6DD1CE26F0B00E5FDF7 /* Pattern1.storyboard */, - 935BD6DE1CE26F0B00E5FDF7 /* Pattern1ViewController.swift */, - 935BD6DF1CE26F0B00E5FDF7 /* Pattern2.storyboard */, - 935BD6E01CE26F0B00E5FDF7 /* Pattern2ViewController.swift */, - 935BD6CC1CE26EB600E5FDF7 /* Main.storyboard */, - 935BD6CF1CE26EB600E5FDF7 /* Assets.xcassets */, - 935BD6D11CE26EB600E5FDF7 /* LaunchScreen.storyboard */, 935BD6D41CE26EB600E5FDF7 /* Info.plist */, + 935BD6D11CE26EB600E5FDF7 /* LaunchScreen.storyboard */, + 935BD6CC1CE26EB600E5FDF7 /* Main.storyboard */, + 935BD6DC1CE26F0B00E5FDF7 /* MainViewController.swift */, ); path = Example; sourceTree = ""; }; + ED510E6D1DDD513200470C8B /* Pattern1 */ = { + isa = PBXGroup; + children = ( + 935BD6DD1CE26F0B00E5FDF7 /* Pattern1.storyboard */, + 935BD6DE1CE26F0B00E5FDF7 /* Pattern1ViewController.swift */, + ); + name = Pattern1; + sourceTree = ""; + }; + ED510E6E1DDD513F00470C8B /* Pattern2 */ = { + isa = PBXGroup; + children = ( + 935BD6DF1CE26F0B00E5FDF7 /* Pattern2.storyboard */, + 935BD6E01CE26F0B00E5FDF7 /* Pattern2ViewController.swift */, + ); + name = Pattern2; + sourceTree = ""; + }; EDA05AD11D92901900F7C2C5 /* Frameworks */ = { isa = PBXGroup; children = ( @@ -211,12 +227,12 @@ isa = PBXResourcesBuildPhase; buildActionMask = 2147483647; files = ( - 935BD6E61CE26F0B00E5FDF7 /* Pattern2.storyboard in Resources */, - 935BD6D31CE26EB600E5FDF7 /* LaunchScreen.storyboard in Resources */, 935BD6D01CE26EB600E5FDF7 /* Assets.xcassets in Resources */, - 935BD6E41CE26F0B00E5FDF7 /* Pattern1.storyboard in Resources */, 935BD6E21CE26F0B00E5FDF7 /* ImageCollectionViewCell.xib in Resources */, + 935BD6D31CE26EB600E5FDF7 /* LaunchScreen.storyboard in Resources */, 935BD6CE1CE26EB600E5FDF7 /* Main.storyboard in Resources */, + 935BD6E41CE26F0B00E5FDF7 /* Pattern1.storyboard in Resources */, + 935BD6E61CE26F0B00E5FDF7 /* Pattern2.storyboard in Resources */, ); runOnlyForDeploymentPostprocessing = 0; }; @@ -227,11 +243,11 @@ isa = PBXSourcesBuildPhase; buildActionMask = 2147483647; files = ( + 935BD6C91CE26EB600E5FDF7 /* AppDelegate.swift in Sources */, + 935BD6E11CE26F0B00E5FDF7 /* ImageCollectionViewCell.swift in Sources */, 935BD6E31CE26F0B00E5FDF7 /* MainViewController.swift in Sources */, 935BD6E51CE26F0B00E5FDF7 /* Pattern1ViewController.swift in Sources */, - 935BD6E11CE26F0B00E5FDF7 /* ImageCollectionViewCell.swift in Sources */, 935BD6E71CE26F0B00E5FDF7 /* Pattern2ViewController.swift in Sources */, - 935BD6C91CE26EB600E5FDF7 /* AppDelegate.swift in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; diff --git a/Example/Example/ImageCollectionViewCell.swift b/Example/Example/ImageCollectionViewCell.swift index e4dd723..c64ba1a 100644 --- a/Example/Example/ImageCollectionViewCell.swift +++ b/Example/Example/ImageCollectionViewCell.swift @@ -12,7 +12,7 @@ final class ImageCollectionViewCell: UICollectionViewCell { @IBOutlet weak var imageView: UIImageView! static let identifier = "ImageCollectionViewCell" static let nib = UINib(nibName: "ImageCollectionViewCell", bundle: nil) - func configure(_ indexPath: IndexPath) { + func configure(indexPath: IndexPath) { let image = UIImage(named: String(indexPath.item)) imageView.image = image setNeedsLayout() diff --git a/Example/Example/MainViewController.swift b/Example/Example/MainViewController.swift index 24d55af..7159d44 100644 --- a/Example/Example/MainViewController.swift +++ b/Example/Example/MainViewController.swift @@ -12,23 +12,26 @@ import InfiniteCollectionView final class MainViewController: UIViewController { var patterns = ["pattern1", "pattern2"] let identifier = "tableViewCell" + var cellHeight: CGFloat { + return (UIScreen.main.bounds.height - 64) / CGFloat(patterns.count) + } @IBOutlet weak var tableView: UITableView! { didSet { tableView.delegate = self tableView.dataSource = self tableView.register(UITableViewCell.self, forCellReuseIdentifier: identifier) - tableView.rowHeight = 100 - tableView.estimatedRowHeight = 100 + tableView.rowHeight = cellHeight + tableView.estimatedRowHeight = cellHeight } } override func viewWillAppear(_ animated: Bool) { super.viewWillAppear(animated) - if let selectedIndexPath = tableView.indexPathForSelectedRow { - tableView.deselectRow(at: selectedIndexPath, animated: true) - } + guard let selectedIndexPath = tableView.indexPathForSelectedRow else { return } + tableView.deselectRow(at: selectedIndexPath, animated: true) } } +// MARK: - UITableViewDelegate, UITableViewDataSource extension MainViewController: UITableViewDelegate, UITableViewDataSource { func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return patterns.count @@ -36,15 +39,20 @@ extension MainViewController: UITableViewDelegate, UITableViewDataSource { func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: identifier)! cell.textLabel?.text = patterns[indexPath.row] + cell.textLabel?.textAlignment = .center + cell.textLabel?.font = UIFont.boldSystemFont(ofSize: 24) return cell } func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { - if indexPath.row == 0 { + switch indexPath.row { + case 0: let controller = Pattern1ViewController.createFromStoryboard() navigationController?.pushViewController(controller, animated: true) - } else if indexPath.row == 1 { + case 1: let controller = Pattern2ViewController.createFromStoryboard() navigationController?.pushViewController(controller, animated: true) + default: + break } } } diff --git a/Example/Example/Pattern1.storyboard b/Example/Example/Pattern1.storyboard index 6f36083..1ef5095 100644 --- a/Example/Example/Pattern1.storyboard +++ b/Example/Example/Pattern1.storyboard @@ -1,11 +1,11 @@ - + - + diff --git a/Example/Example/Pattern1ViewController.swift b/Example/Example/Pattern1ViewController.swift index 7c68f7f..632a329 100644 --- a/Example/Example/Pattern1ViewController.swift +++ b/Example/Example/Pattern1ViewController.swift @@ -10,12 +10,11 @@ import UIKit import InfiniteCollectionView final class Pattern1ViewController: UIViewController { - var items = ["1", "2", "3", "4"] + var itemsCount: Int = 5 @IBOutlet weak var collectionView: InfiniteCollectionView! { didSet { collectionView.infiniteDataSource = self collectionView.infiniteDelegate = self - collectionView.cellWidth = UIScreen.main.bounds.width collectionView.register(ImageCollectionViewCell.nib, forCellWithReuseIdentifier: ImageCollectionViewCell.identifier) } } @@ -26,7 +25,7 @@ final class Pattern1ViewController: UIViewController { } @IBOutlet weak var pageControl: UIPageControl! { didSet { - pageControl.numberOfPages = items.count + pageControl.numberOfPages = itemsCount } } static func createFromStoryboard() -> Pattern1ViewController { @@ -37,7 +36,6 @@ final class Pattern1ViewController: UIViewController { super.viewWillTransition(to: size, with: coordinator) layout.itemSize = size layout.invalidateLayout() - collectionView.cellWidth = size.width collectionView.layoutIfNeeded() collectionView.setNeedsLayout() } @@ -46,11 +44,11 @@ final class Pattern1ViewController: UIViewController { // MARK: - InfiniteCollectionViewDataSource, InfiniteCollectionViewDelegate extension Pattern1ViewController: InfiniteCollectionViewDataSource, InfiniteCollectionViewDelegate { func number(ofItems collectionView: UICollectionView) -> Int { - return items.count + return itemsCount } func collectionView(_ collectionView: UICollectionView, dequeueForItemAt dequeueIndexPath: IndexPath, cellForItemAt usableIndexPath: IndexPath) -> UICollectionViewCell { let cell = collectionView.dequeueReusableCell(withReuseIdentifier: ImageCollectionViewCell.identifier, for: dequeueIndexPath) as! ImageCollectionViewCell - cell.configure(usableIndexPath) + cell.configure(indexPath: usableIndexPath) return cell } func infiniteCollectionView(_ collectionView: UICollectionView, didSelectItemAt usableIndexPath: IndexPath) { diff --git a/Example/Example/Pattern2.storyboard b/Example/Example/Pattern2.storyboard index 1c3b140..a0e9a54 100644 --- a/Example/Example/Pattern2.storyboard +++ b/Example/Example/Pattern2.storyboard @@ -1,11 +1,11 @@ - + - + @@ -25,15 +25,15 @@ - - + + - + - + @@ -43,14 +43,14 @@ - + - + - + @@ -60,7 +60,7 @@ - + @@ -68,14 +68,14 @@ - + - + @@ -102,15 +102,15 @@ - - + + - + - + @@ -120,7 +120,7 @@ - + diff --git a/Example/Example/Pattern2ViewController.swift b/Example/Example/Pattern2ViewController.swift index a4c5382..7c70997 100644 --- a/Example/Example/Pattern2ViewController.swift +++ b/Example/Example/Pattern2ViewController.swift @@ -10,12 +10,10 @@ import UIKit import InfiniteCollectionView final class Pattern2ViewController: UIViewController { - static func createFromStoryboard() -> Pattern2ViewController { let storyboard = UIStoryboard(name: "Pattern2", bundle: nil) return storyboard.instantiateInitialViewController() as! Pattern2ViewController } - @IBOutlet weak var tableView: UITableView! { didSet { tableView.delegate = self @@ -25,38 +23,35 @@ final class Pattern2ViewController: UIViewController { } } - // MARK: - UITableViewDataSource, UITableViewDelegate extension Pattern2ViewController: UITableViewDataSource, UITableViewDelegate { func numberOfSections(in tableView: UITableView) -> Int { return 2 } func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { - if section == 0 { - return 1 - } else if section == 1 { - return 1 + switch section { + case 0, 1: return 1 + default: return 0 } - return 0 } func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { - if indexPath.section == 0 { + switch indexPath.section { + case 0: let cell = tableView.dequeueReusableCell(withIdentifier: InfiniteTableViewCell.identifier) as! InfiniteTableViewCell return cell - } else if indexPath.section == 1 { + case 1: let cell = tableView.dequeueReusableCell(withIdentifier: Infinite2TableViewCell.identifier) as! Infinite2TableViewCell return cell - } else { + default: return UITableViewCell() } } func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { - if indexPath.section == 0 { - return 240 - } else if indexPath.section == 1 { - return 120 + switch indexPath.section { + case 0: return 240 + case 1: return 120 + default: return 0 } - return 0 } } @@ -64,16 +59,15 @@ final class InfiniteTableViewCell: UITableViewCell { static let identifier = "InfiniteTableViewCell" required init?(coder aDecoder: NSCoder) { super.init(coder: aDecoder) - NotificationCenter.default.addObserver(self, selector: #selector(InfiniteTableViewCell.rotate(_:)), name: NSNotification.Name.UIDeviceOrientationDidChange, object: nil) + NotificationCenter.default.addObserver(self, selector: #selector(InfiniteTableViewCell.rotate(_:)), name: .UIDeviceOrientationDidChange, object: nil) } deinit { - NotificationCenter.default.removeObserver(self, name: NSNotification.Name.UIDeviceOrientationDidChange, object: nil) + NotificationCenter.default.removeObserver(self, name: .UIDeviceOrientationDidChange, object: nil) } @IBOutlet weak var collectionView: InfiniteCollectionView! { didSet { collectionView.infiniteDataSource = self collectionView.infiniteDelegate = self - collectionView.cellWidth = UIScreen.main.bounds.width collectionView.register(ImageCollectionViewCell.nib, forCellWithReuseIdentifier: ImageCollectionViewCell.identifier) } } @@ -91,7 +85,7 @@ final class InfiniteTableViewCell: UITableViewCell { let size = CGSize(width: UIScreen.main.bounds.width, height: 239) layout.itemSize = size layout.invalidateLayout() - collectionView.cellWidth = size.width + collectionView.rotate(notification) collectionView.layoutIfNeeded() collectionView.setNeedsLayout() } @@ -104,7 +98,7 @@ extension InfiniteTableViewCell: InfiniteCollectionViewDataSource, InfiniteColle } func collectionView(_ collectionView: UICollectionView, dequeueForItemAt dequeueIndexPath: IndexPath, cellForItemAt usableIndexPath: IndexPath) -> UICollectionViewCell { let cell = collectionView.dequeueReusableCell(withReuseIdentifier: ImageCollectionViewCell.identifier, for: dequeueIndexPath) as! ImageCollectionViewCell - cell.configure(usableIndexPath) + cell.configure(indexPath: usableIndexPath) return cell } func scrollView(_ scrollView: UIScrollView, pageIndex: Int) { @@ -116,16 +110,15 @@ final class Infinite2TableViewCell: UITableViewCell { static let identifier = "Infinite2TableViewCell" required init?(coder aDecoder: NSCoder) { super.init(coder: aDecoder) - NotificationCenter.default.addObserver(self, selector: #selector(Infinite2TableViewCell.rotate(_:)), name: NSNotification.Name.UIDeviceOrientationDidChange, object: nil) + NotificationCenter.default.addObserver(self, selector: #selector(Infinite2TableViewCell.rotate(_:)), name: .UIDeviceOrientationDidChange, object: nil) } deinit { - NotificationCenter.default.removeObserver(self, name: NSNotification.Name.UIDeviceOrientationDidChange, object: nil) + NotificationCenter.default.removeObserver(self, name: .UIDeviceOrientationDidChange, object: nil) } @IBOutlet weak var collectionView: InfiniteCollectionView! { didSet { collectionView.infiniteDataSource = self collectionView.infiniteDelegate = self - collectionView.cellWidth = 100 collectionView.register(ImageCollectionViewCell.nib, forCellWithReuseIdentifier: ImageCollectionViewCell.identifier) } } @@ -138,12 +131,16 @@ extension Infinite2TableViewCell: InfiniteCollectionViewDataSource, InfiniteColl } func collectionView(_ collectionView: UICollectionView, dequeueForItemAt dequeueIndexPath: IndexPath, cellForItemAt usableIndexPath: IndexPath) -> UICollectionViewCell { let cell = collectionView.dequeueReusableCell(withReuseIdentifier: ImageCollectionViewCell.identifier, for: dequeueIndexPath) as! ImageCollectionViewCell - cell.configure(usableIndexPath) + cell.configure(indexPath: usableIndexPath) return cell } func infiniteCollectionView(_ collectionView: UICollectionView, didSelectItemAt usableIndexPath: IndexPath) { print("didSelectItemAt: \(usableIndexPath.item)") } func rotate(_ notification: Notification) { + collectionView.collectionViewLayout.invalidateLayout() + collectionView.rotate(notification) + collectionView.layoutIfNeeded() + collectionView.setNeedsLayout() } } diff --git a/InfiniteCollectionView.podspec b/InfiniteCollectionView.podspec index d9ec92c..4b6c491 100644 --- a/InfiniteCollectionView.podspec +++ b/InfiniteCollectionView.podspec @@ -1,6 +1,6 @@ Pod::Spec.new do |s| s.name = "InfiniteCollectionView" - s.version = "1.1.0" + s.version = "1.2.0" s.summary = "Infinite Scrolling Using UICollectionView." s.homepage = "https://github.com/hryk224/InfiniteCollectionView" s.screenshots = "https://github.com/hryk224/InfiniteCollectionView/wiki/images/sample1.gif" diff --git a/InfiniteCollectionView/InfiniteCollectionView.swift b/InfiniteCollectionView/InfiniteCollectionView.swift index 4b70692..783a1da 100644 --- a/InfiniteCollectionView/InfiniteCollectionView.swift +++ b/InfiniteCollectionView/InfiniteCollectionView.swift @@ -32,14 +32,8 @@ open class InfiniteCollectionView: UICollectionView { fileprivate static let defaultIdentifier = "Cell" open weak var infiniteDataSource: InfiniteCollectionViewDataSource? open weak var infiniteDelegate: InfiniteCollectionViewDelegate? - open var cellWidth: CGFloat = UIScreen.main.bounds.width { - didSet { - contentWidth = totalContentWidth - setContentOffset(CGPoint(x: CGFloat(pageIndex + indexOffset) * cellWidth, y: contentOffset.y), animated: false) - } - } fileprivate var indexOffset: Int = 0 - fileprivate var contentWidth: CGFloat = 0 + fileprivate var contentWidth: CGFloat = UIScreen.main.bounds.width fileprivate var pageIndex = 0 required public init?(coder aDecoder: NSCoder) { super.init(coder: aDecoder) @@ -56,16 +50,21 @@ open class InfiniteCollectionView: UICollectionView { super.reloadData() contentWidth = totalContentWidth } - func rotate(_ notification: Notification) { + open func rotate(_ notification: Notification) { contentWidth = totalContentWidth + contentOffset = CGPoint(x: CGFloat(pageIndex + indexOffset) * cellWidth, y: contentOffset.y) } } // MARK: - private private extension InfiniteCollectionView { + var cellWidth: CGFloat { + guard let layout = collectionViewLayout as? UICollectionViewFlowLayout else { return 0 } + return layout.itemSize.width + layout.minimumInteritemSpacing + } var totalContentWidth: CGFloat { - let numberOfCells = infiniteDataSource?.number(ofItems: self) ?? 0 - return CGFloat(numberOfCells) * cellWidth + let numberOfCells: CGFloat = CGFloat(infiniteDataSource?.number(ofItems: self) ?? 0) + return numberOfCells * cellWidth } func configure() { delegate = self @@ -75,7 +74,7 @@ private extension InfiniteCollectionView { } func centerIfNeeded(_ scrollView: UIScrollView) { let currentOffset = contentOffset - let centerX = (CGFloat(Me.dummyCount) * contentWidth - bounds.width) / 2 + let centerX = (scrollView.contentSize.width - bounds.width) / 2 let distFromCenter = centerX - currentOffset.x if fabs(distFromCenter) > (contentWidth / 4) { let cellcount = distFromCenter / cellWidth diff --git a/README.md b/README.md index a4f3690..b254f5a 100644 --- a/README.md +++ b/README.md @@ -6,6 +6,7 @@ Infinite horizontal scrolling using `UICollectionView` [![Swift 3.0](https://img.shields.io/badge/Swift-3.0-orange.svg?style=flat)](https://developer.apple.com/swift/) + ## Requirements @@ -43,7 +44,6 @@ import InfiniteCollectionView ```Swift collectionView.infiniteDataSource = XXX collectionView.infiniteDelegate = XXX -collectionView.cellWidth = XXX ``` ```Swift