From 8015d917e677786d8d688366ec32c25605811230 Mon Sep 17 00:00:00 2001 From: Max Adamyan Date: Tue, 15 Nov 2016 16:40:11 +0400 Subject: [PATCH 1/2] Fix Delegate Fixed delegate didSet observer in AKPickerView. --- AKPickerView/AKPickerView.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/AKPickerView/AKPickerView.swift b/AKPickerView/AKPickerView.swift index c8370c2..12303c0 100644 --- a/AKPickerView/AKPickerView.swift +++ b/AKPickerView/AKPickerView.swift @@ -233,7 +233,7 @@ public class AKPickerView: UIView, UICollectionViewDataSource, UICollectionViewD public weak var dataSource: AKPickerViewDataSource? = nil /// Readwrite. Delegate of picker view. public weak var delegate: AKPickerViewDelegate? = nil { - didSet(delegate) { + didSet { self.intercepter.delegate = delegate } } From 011935a74bb38fb48e0e59506982561dcd0c4110 Mon Sep 17 00:00:00 2001 From: Max Adamyan Date: Wed, 16 Nov 2016 11:42:30 +0400 Subject: [PATCH 2/2] Smoother scrolling. Scrolling stops (decelerates) always on item center. --- AKPickerView/AKPickerView.swift | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/AKPickerView/AKPickerView.swift b/AKPickerView/AKPickerView.swift index 12303c0..32e6e2a 100644 --- a/AKPickerView/AKPickerView.swift +++ b/AKPickerView/AKPickerView.swift @@ -181,6 +181,20 @@ private class AKCollectionViewLayout: UICollectionViewFlowLayout { return attributes } } + + override func targetContentOffset(forProposedContentOffset proposedContentOffset: CGPoint, withScrollingVelocity velocity: CGPoint) -> CGPoint { + var offsetAdjustment: CGFloat = CGFloat.greatestFiniteMagnitude + let horizontalCenter: CGFloat = proposedContentOffset.x + (self.collectionView!.bounds.width / 2.0) + let targetRect = CGRect(x: proposedContentOffset.x, y: 0.0, width: self.collectionView!.bounds.size.width, height: self.collectionView!.bounds.size.height) + let array:[UICollectionViewLayoutAttributes] = self.layoutAttributesForElements(in: targetRect)! + for layoutAttributes: UICollectionViewLayoutAttributes in array { + let itemHorizontalCenter: CGFloat = layoutAttributes.center.x + if abs(itemHorizontalCenter - horizontalCenter) < abs(offsetAdjustment) { + offsetAdjustment = itemHorizontalCenter - horizontalCenter + } + } + return CGPoint(x: proposedContentOffset.x + offsetAdjustment, y: proposedContentOffset.y) + } }