From c3a7a39f45bf0c586890be0e8b71d318e8d9bbaf Mon Sep 17 00:00:00 2001 From: Kevin McGill Date: Fri, 21 Jul 2017 22:17:34 -0500 Subject: [PATCH 1/7] WIP: adding ability to set initial positions of rows --- Example/McPicker/ViewController.swift | 4 ++++ McPicker/Classes/McPicker.swift | 14 ++++++++++++++ 2 files changed, 18 insertions(+) diff --git a/Example/McPicker/ViewController.swift b/Example/McPicker/ViewController.swift index bbfc9fc..b1b5e9b 100644 --- a/Example/McPicker/ViewController.swift +++ b/Example/McPicker/ViewController.swift @@ -82,6 +82,10 @@ class ViewController: UIViewController { mcPicker.toolbarButtonsColor = .white mcPicker.toolbarBarTintColor = .darkGray mcPicker.pickerBackgroundColor = .gray + mcPicker.pickerSelectRowsForComponents = [ + 0: [1: true], + 1: [2: true] + ] if let barButton = sender as? UIBarButtonItem { // Show as Popover diff --git a/McPicker/Classes/McPicker.swift b/McPicker/Classes/McPicker.swift index e4ad19f..af746ee 100644 --- a/McPicker/Classes/McPicker.swift +++ b/McPicker/Classes/McPicker.swift @@ -60,6 +60,20 @@ open class McPicker: UIView { picker.backgroundColor = pickerBackgroundColor } } + /** + Set the pickers default components positions. + + [Int:[Int:Bool]] equates to [Component: [Row: isAnimated] + */ + public var pickerSelectRowsForComponents: [Int: [Int: Bool]]? { + didSet { + for component in pickerSelectRowsForComponents!.keys { + if let row = pickerSelectRowsForComponents![component]?.keys.first, let isAnimated = pickerSelectRowsForComponents![component]?.values.first { + picker.selectRow(row, inComponent: component, animated: isAnimated) + } + } + } + } internal var popOverContentSize: CGSize { return CGSize(width: Constant.pickerHeight + Constant.toolBarHeight, height: Constant.pickerHeight + Constant.toolBarHeight) From 1d154fe32b35b4978abac708204f2a26a35b6ef1 Mon Sep 17 00:00:00 2001 From: Kevin McGill Date: Fri, 21 Jul 2017 22:32:31 -0500 Subject: [PATCH 2/7] Set defaults selections to default rows. --- Example/Tests/McPickerTests.swift | 28 ++++++++++++++++++++++++++-- McPicker/Classes/McPicker.swift | 2 ++ 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/Example/Tests/McPickerTests.swift b/Example/Tests/McPickerTests.swift index 5a27580..b20a98c 100644 --- a/Example/Tests/McPickerTests.swift +++ b/Example/Tests/McPickerTests.swift @@ -166,7 +166,7 @@ class McPickerTests: XCTestCase { XCTAssertEqual(UIColor.purple, mcPicker.picker.backgroundColor) } - func testDimissViews_callsAnimateViews() { + func testDimissViews_callsAnimateViewsWhenShow() { // Given // class TestMcPicker: McPicker { @@ -196,7 +196,7 @@ class McPickerTests: XCTestCase { XCTAssertEqual(McPicker.AnimationDirection.out, mcPicker.direction) } - func testDimissViews_calls() { + func testDimissViews_doesNotCallAnimateViewsWhenShowAsPopover() { // Given // class TestVC: UIViewController { @@ -227,4 +227,28 @@ class McPickerTests: XCTestCase { XCTAssertNil(mcPicker.mcPickerPopoverViewController) XCTAssertFalse(mcPicker.calledAnimateViews) } + + func testPickerSelectRowsForComponents() { + // Given + // + let data: [[String]] = [ + ["Sir", "Mr", "Mrs", "Miss"], + ["Kevin", "Lauren", "Kibby", "Stella"] + ] + let mcPicker = McPicker(data: data) + + // When + // + mcPicker.pickerSelectRowsForComponents = [ + 0: [3: true], + 1: [2: true] + ] + + // Then + // + XCTAssertEqual(3, mcPicker.picker.selectedRow(inComponent: 0)) + XCTAssertEqual(2, mcPicker.picker.selectedRow(inComponent: 1)) + XCTAssertEqual("Miss", mcPicker.pickerSelection[0]!) + XCTAssertEqual("Kibby", mcPicker.pickerSelection[1]!) + } } diff --git a/McPicker/Classes/McPicker.swift b/McPicker/Classes/McPicker.swift index af746ee..243104c 100644 --- a/McPicker/Classes/McPicker.swift +++ b/McPicker/Classes/McPicker.swift @@ -60,6 +60,7 @@ open class McPicker: UIView { picker.backgroundColor = pickerBackgroundColor } } + /** Set the pickers default components positions. @@ -69,6 +70,7 @@ open class McPicker: UIView { didSet { for component in pickerSelectRowsForComponents!.keys { if let row = pickerSelectRowsForComponents![component]?.keys.first, let isAnimated = pickerSelectRowsForComponents![component]?.values.first { + pickerSelection[component] = pickerData[component][row] picker.selectRow(row, inComponent: component, animated: isAnimated) } } From f44d7f6511165f27efe57dbec3219c24298b9d57 Mon Sep 17 00:00:00 2001 From: Kevin McGill Date: Fri, 21 Jul 2017 22:41:30 -0500 Subject: [PATCH 3/7] Update description --- McPicker/Classes/McPicker.swift | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/McPicker/Classes/McPicker.swift b/McPicker/Classes/McPicker.swift index 243104c..47fdb33 100644 --- a/McPicker/Classes/McPicker.swift +++ b/McPicker/Classes/McPicker.swift @@ -60,9 +60,8 @@ open class McPicker: UIView { picker.backgroundColor = pickerBackgroundColor } } - /** - Set the pickers default components positions. + Sets the picker's components row position and picker selections to those String values. [Int:[Int:Bool]] equates to [Component: [Row: isAnimated] */ From ab8c14371184539c03441bcbf088b4f42ce0d644 Mon Sep 17 00:00:00 2001 From: Kevin McGill Date: Fri, 21 Jul 2017 22:45:17 -0500 Subject: [PATCH 4/7] Update README --- README.md | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 30383f5..d0f3f7d 100644 --- a/README.md +++ b/README.md @@ -35,15 +35,16 @@ McPicker.showAsPopover(data: data, fromViewController: self, barButtonItem: send #### Customization ```swift +let data: [[String]] = [ + ["Sir", "Mr", "Mrs", "Miss"], + ["Kevin", "Lauren", "Kibby", "Stella"] +] + let customLabel = UILabel() customLabel.textAlignment = .center customLabel.textColor = .white customLabel.font = UIFont(name:"American Typewriter", size: 30)! -let data: [[String]] = [ - ["Sir", "Mr", "Mrs", "Miss"], - ["Kevin", "Lauren", "Kibby", "Stella"] -] let mcPicker = McPicker(data: data) mcPicker.label = customLabel // Set your custom label @@ -51,6 +52,10 @@ mcPicker.toolbarItemsFont = UIFont(name:"American Typewriter", size: 17)! mcPicker.toolbarButtonsColor = .white mcPicker.toolbarBarTintColor = .darkGray mcPicker.pickerBackgroundColor = .gray +mcPicker.pickerSelectRowsForComponents = [ + 0: [1: true], + 1: [2: true] // [Component: [Row: isAnimated] +] if let barButton = sender as? UIBarButtonItem { // Show as Popover From 8fe69492c91d0afd7a56e648bedb1c93e10731e9 Mon Sep 17 00:00:00 2001 From: Kevin McGill Date: Fri, 21 Jul 2017 23:21:16 -0500 Subject: [PATCH 5/7] Bump podspec --- Example/McPicker/ViewController.swift | 2 +- McPicker.podspec | 2 +- README.md | 6 ++++-- 3 files changed, 6 insertions(+), 4 deletions(-) diff --git a/Example/McPicker/ViewController.swift b/Example/McPicker/ViewController.swift index b1b5e9b..2c285d6 100644 --- a/Example/McPicker/ViewController.swift +++ b/Example/McPicker/ViewController.swift @@ -83,7 +83,7 @@ class ViewController: UIViewController { mcPicker.toolbarBarTintColor = .darkGray mcPicker.pickerBackgroundColor = .gray mcPicker.pickerSelectRowsForComponents = [ - 0: [1: true], + 0: [3: true], 1: [2: true] ] diff --git a/McPicker.podspec b/McPicker.podspec index 7709f53..6f0e779 100644 --- a/McPicker.podspec +++ b/McPicker.podspec @@ -1,6 +1,6 @@ Pod::Spec.new do |s| s.name = 'McPicker' - s.version = '0.3.1' + s.version = '0.4.0' s.summary = 'McPicker is a UIPickerView drop-in solution with animations that is rotation ready.' s.description = <<-DESC diff --git a/README.md b/README.md index d0f3f7d..41d7660 100644 --- a/README.md +++ b/README.md @@ -45,7 +45,6 @@ customLabel.textAlignment = .center customLabel.textColor = .white customLabel.font = UIFont(name:"American Typewriter", size: 30)! - let mcPicker = McPicker(data: data) mcPicker.label = customLabel // Set your custom label mcPicker.toolbarItemsFont = UIFont(name:"American Typewriter", size: 17)! @@ -53,7 +52,7 @@ mcPicker.toolbarButtonsColor = .white mcPicker.toolbarBarTintColor = .darkGray mcPicker.pickerBackgroundColor = .gray mcPicker.pickerSelectRowsForComponents = [ - 0: [1: true], + 0: [3: true], 1: [2: true] // [Component: [Row: isAnimated] ] @@ -76,6 +75,9 @@ if let barButton = sender as? UIBarButtonItem { } ``` +##### The `selections` +McPicker's `doneHandler` passes back `selections: [Int : String]` as an argument. This is as simple as `[: ]`. + ## Requirements - iOS 8+ - Swift 3+ From 7dbf8049ca6524133905ee380cbf339032ac1277 Mon Sep 17 00:00:00 2001 From: Kevin McGill Date: Fri, 21 Jul 2017 23:21:29 -0500 Subject: [PATCH 6/7] Update README --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 41d7660..f2aca4b 100644 --- a/README.md +++ b/README.md @@ -76,7 +76,7 @@ if let barButton = sender as? UIBarButtonItem { ``` ##### The `selections` -McPicker's `doneHandler` passes back `selections: [Int : String]` as an argument. This is as simple as `[: ]`. +McPicker's `doneHandler` passes back `selections: [Int : String]` as an argument. This is as simple as `[: ]`. ## Requirements - iOS 8+ From f8feb1bdba8b0bbd317d6c652861eca71b358c25 Mon Sep 17 00:00:00 2001 From: Kevin McGill Date: Fri, 21 Jul 2017 23:28:19 -0500 Subject: [PATCH 7/7] More README --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index f2aca4b..d90c5d8 100644 --- a/README.md +++ b/README.md @@ -76,7 +76,7 @@ if let barButton = sender as? UIBarButtonItem { ``` ##### The `selections` -McPicker's `doneHandler` passes back `selections: [Int : String]` as an argument. This is as simple as `[: ]`. +McPicker's `doneHandler` passes back `selections: [Int : String]` as an argument. This is as simple as `[: ]` from the `data` you've passed in. ## Requirements - iOS 8+