6 Commits

Author SHA1 Message Date
hryk224 bcc954d105 Update podspec 2017-01-12 10:40:48 +09:00
hryk224 543b75cf56 Merge pull request #11 from jwardle/master
selectItem implementation
2017-01-12 10:38:49 +09:00
James Wardle 4bd0209ccb Update InfiniteCollectionView.swift
~ Enhanced the implementation to correct the index.
2017-01-10 21:59:52 +00:00
James Wardle 6fcd6a52e2 Update InfiniteCollectionView.swift
+ Added selectItem override to allow programatic selection of items - previously this was not considered and broken.
2017-01-09 22:01:02 +00:00
hryk224 d5afefb36d Updated 1.2.1 2017-01-05 11:14:02 +09:00
hryk224 118f9c3a45 Small changed 2017-01-05 11:12:06 +09:00
4 changed files with 36 additions and 13 deletions
+10 -3
View File
@@ -28,14 +28,21 @@ final class Pattern1ViewController: UIViewController {
pageControl.numberOfPages = itemsCount
}
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
NotificationCenter.default.addObserver(self, selector: #selector(Pattern1ViewController.rotate(_:)), name: .UIDeviceOrientationDidChange, object: nil)
}
deinit {
NotificationCenter.default.removeObserver(self, name: .UIDeviceOrientationDidChange, object: nil)
}
static func createFromStoryboard() -> Pattern1ViewController {
let storyboard = UIStoryboard(name: "Pattern1", bundle: nil)
return storyboard.instantiateInitialViewController() as! Pattern1ViewController
}
override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
super.viewWillTransition(to: size, with: coordinator)
layout.itemSize = size
func rotate(_ notification: Notification) {
layout.itemSize = UIScreen.main.bounds.size
layout.invalidateLayout()
collectionView.rotate(notification)
collectionView.layoutIfNeeded()
collectionView.setNeedsLayout()
}
+1 -1
View File
@@ -1,6 +1,6 @@
Pod::Spec.new do |s|
s.name = "InfiniteCollectionView"
s.version = "1.2.0"
s.version = "1.3.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"
@@ -33,7 +33,6 @@ open class InfiniteCollectionView: UICollectionView {
open weak var infiniteDataSource: InfiniteCollectionViewDataSource?
open weak var infiniteDelegate: InfiniteCollectionViewDelegate?
fileprivate var indexOffset: Int = 0
fileprivate var contentWidth: CGFloat = UIScreen.main.bounds.width
fileprivate var pageIndex = 0
@available(*, deprecated, message: "It becomes unnecessary because it uses UICollectionViewFlowLayout.")
open var cellWidth: CGFloat?
@@ -48,13 +47,30 @@ open class InfiniteCollectionView: UICollectionView {
deinit {
NotificationCenter.default.removeObserver(self, name: .UIDeviceOrientationDidChange, object: nil)
}
open override func reloadData() {
super.reloadData()
contentWidth = totalContentWidth
}
open func rotate(_ notification: Notification) {
contentWidth = totalContentWidth
contentOffset = CGPoint(x: CGFloat(pageIndex + indexOffset) * itemWidth, y: contentOffset.y)
setContentOffset(CGPoint(x: CGFloat(pageIndex + indexOffset) * itemWidth, y: contentOffset.y), animated: false)
}
open override func selectItem(at indexPath: IndexPath?, animated: Bool, scrollPosition: UICollectionViewScrollPosition) {
// Correct the input IndexPath
let correctedIndexPath = IndexPath(row: correctedIndex(indexPath!.item + indexOffset), section: 0);
// Get the currently visible cell(s) - assumes a cell is visible
guard let visibleCell = self.visibleCells.first else{
return; // Nothing to select...
}
// Index path of the cell - does not consider multiple cells on the screen at the same time
var visibleIndexPath : IndexPath! = self.indexPath(for: visibleCell);
let testIndexPath = IndexPath(row: correctedIndex(visibleIndexPath!.item), section: 0);
guard correctedIndexPath != testIndexPath else{
return; // Do not re-select the same cell
}
// Call supercase to select the correct IndexPath
super.selectItem(at: correctedIndexPath, animated: animated, scrollPosition: scrollPosition);
}
}
@@ -78,7 +94,7 @@ private extension InfiniteCollectionView {
let currentOffset = contentOffset
let centerX = (scrollView.contentSize.width - bounds.width) / 2
let distFromCenter = centerX - currentOffset.x
if fabs(distFromCenter) > (contentWidth / 4) {
if fabs(distFromCenter) > (totalContentWidth / 4) {
let cellcount = distFromCenter / itemWidth
let shiftCells = Int((cellcount > 0) ? floor(cellcount) : ceil(cellcount))
let offsetCorrection = (abs(cellcount).truncatingRemainder(dividingBy: 1)) * itemWidth
+1 -1
View File
@@ -15,7 +15,7 @@
<key>CFBundlePackageType</key>
<string>FMWK</string>
<key>CFBundleShortVersionString</key>
<string>1.2.0</string>
<string>1.3.0</string>
<key>CFBundleSignature</key>
<string>????</string>
<key>CFBundleVersion</key>