Skip to content

Commit

Permalink
adds back the frame adjustments for item 0 && numberOfRowsInPickerVie…
Browse files Browse the repository at this point in the history
…w - 1
  • Loading branch information
Ace Green authored and acegreen committed Aug 13, 2017
1 parent f7320c5 commit 4f70d2c
Showing 1 changed file with 63 additions and 8 deletions.
71 changes: 63 additions & 8 deletions Pod/Classes/PickerView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,7 @@ open class PickerView: UIView {
}

fileprivate func adjustSelectionOverlayHeightConstraint() {

if selectionOverlayH.constant != rowHeight || selectionImageH.constant != rowHeight || selectionIndicatorB.constant != (rowHeight / 2) {
selectionOverlayH.constant = rowHeight
selectionImageH.constant = rowHeight
Expand Down Expand Up @@ -464,7 +465,13 @@ open class PickerView: UIView {
collectionView.contentInset = UIEdgeInsets.zero

let indexOfSelectedRow = visibleIndexOfSelectedRow()
collectionView.setContentOffset(CGPoint(x: 0.0, y: CGFloat(indexOfSelectedRow) * rowHeight), animated: false)

switch scrollingDirection {
case .horizontal:
collectionView.setContentOffset(CGPoint(x: CGFloat(indexOfSelectedRow) * rowHeight, y: 0.0), animated: false)
case .vertical:
collectionView.setContentOffset(CGPoint(x: 0.0, y: CGFloat(indexOfSelectedRow) * rowHeight), animated: false)
}

delegate?.pickerView?(self, didSelectRow: currentSelectedRow, index: currentSelectedIndex)

Expand Down Expand Up @@ -509,7 +516,7 @@ open class PickerView: UIView {
let middleIndex = numberOfRowsByDataSource * middleMultiplier
let indexForSelectedRow: Int

if let _ = currentSelectedRow , scrollingStyle == .default && currentSelectedRow == 0 {
if let _ = currentSelectedRow, scrollingStyle == .default && currentSelectedRow == 0 {
indexForSelectedRow = 0
} else if let _ = currentSelectedRow {
indexForSelectedRow = middleIndex - (numberOfRowsByDataSource - currentSelectedRow)
Expand All @@ -535,7 +542,12 @@ open class PickerView: UIView {

delegate?.pickerView?(self, didSelectRow: currentSelectedRow, index: currentSelectedIndex)

collectionView.setContentOffset(CGPoint(x: 0.0, y: CGFloat(finalRow) * rowHeight), animated: animated)
switch scrollingDirection {
case .horizontal:
collectionView.setContentOffset(CGPoint(x: CGFloat(finalRow) * rowHeight, y: 0.0), animated: animated)
case .vertical:
collectionView.setContentOffset(CGPoint(x: 0.0, y: CGFloat(finalRow) * rowHeight), animated: animated)
}
}

open func reloadPickerView() {
Expand All @@ -544,7 +556,7 @@ open class PickerView: UIView {

}

extension PickerView: UICollectionViewDataSource {
extension PickerView: UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {

// MARK: UIcollectiongViewDataSource

Expand All @@ -556,6 +568,28 @@ extension PickerView: UICollectionViewDataSource {
return numberOfRowsByDataSource * infinityRowsMultiplier
}

public func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
let numberOfRowsInPickerView = dataSource!.pickerViewNumberOfRows(self) * infinityRowsMultiplier

func getSize() -> CGSize {
switch scrollingDirection {
case .horizontal:
return CGSize(width: (frame.width / 2) + (rowHeight / 2), height: layout.itemSize.height)
case .vertical:
return CGSize(width: layout.itemSize.width, height: (frame.height / 2) + (rowHeight / 2))
}
}

// When the scrolling reach the end on top/bottom we need to set the first/last row to appear in the center of PickerView, so that row must be bigger.
let size = getSize()
if (indexPath as NSIndexPath).row == 0 {
return size
} else if numberOfRowsInPickerView > 0 && (indexPath as NSIndexPath).row == numberOfRowsInPickerView - 1 {
return size
}
return layout.itemSize
}

public func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

let indexOfSelectedRow = visibleIndexOfSelectedRow()
Expand All @@ -568,7 +602,12 @@ extension PickerView: UICollectionViewDataSource {

if (view != nil) {
var frame = view!.frame
frame.origin.y = (indexPath as NSIndexPath).row == 0 ? (self.frame.height / 2) - (rowHeight / 2) : 0.0
switch scrollingDirection {
case .horizontal:
frame.origin.x = (indexPath as NSIndexPath).row == 0 ? (self.frame.width / 2) - (rowHeight / 2) : 0.0
case .vertical:
frame.origin.y = (indexPath as NSIndexPath).row == 0 ? (self.frame.height / 2) - (rowHeight / 2) : 0.0
}
view!.frame = frame
pickerViewCell.customView = view
pickerViewCell.contentView.addSubview(pickerViewCell.customView!)
Expand Down Expand Up @@ -608,13 +647,20 @@ extension PickerView: UIScrollViewDelegate {
}

public func scrollViewWillEndDragging(_ scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>) {
let partialRow = Float(targetContentOffset.pointee.y / rowHeight) // Get the estimative of what row will be the selected when the scroll animation ends.

let partialRow = getPartialRow(scrollingDirection: scrollingDirection, contentOffset: targetContentOffset.pointee) // Get the estimative of what row will be the selected when the scroll animation ends.
var roundedRow = Int(lroundf(partialRow)) // Round the estimative to a row

if roundedRow < 0 {
roundedRow = 0
} else {
targetContentOffset.pointee.y = CGFloat(roundedRow) * rowHeight // Set the targetContentOffset (where the scrolling position will be when the animation ends) to a rounded value.

switch scrollingDirection {
case .horizontal:
targetContentOffset.pointee.x = CGFloat(roundedRow) * rowHeight // Set the targetContentOffset (where the scrolling position will be when the animation ends) to a rounded value.
case .vertical:
targetContentOffset.pointee.y = CGFloat(roundedRow) * rowHeight // Set the targetContentOffset (where the scrolling position will be when the animation ends) to a rounded value.
}
}

// Update the currentSelectedRow and notify the delegate that we have a new selected row.
Expand All @@ -634,7 +680,7 @@ extension PickerView: UIScrollViewDelegate {
}

public func scrollViewDidScroll(_ scrollView: UIScrollView) {
let partialRow = Float(scrollView.contentOffset.y / rowHeight)
let partialRow = getPartialRow(scrollingDirection: scrollingDirection, contentOffset: scrollView.contentOffset)
let roundedRow = Int(lroundf(partialRow))

// Avoid to have two highlighted rows at the same time
Expand All @@ -652,4 +698,13 @@ extension PickerView: UIScrollViewDelegate {
}
}

func getPartialRow(scrollingDirection: ScrollingDirection, contentOffset: CGPoint) -> Float {
switch scrollingDirection {
case .horizontal:
return Float(contentOffset.x / rowHeight)
case .vertical:
return Float(contentOffset.y / rowHeight)
}
}

}

0 comments on commit 4f70d2c

Please sign in to comment.