├── EMPickerView_Date_Time.png ├── Examples ├── EMPickerViewExample.xcodeproj │ ├── project.xcworkspace │ │ └── contents.xcworkspacedata │ └── project.pbxproj └── EMPickerViewExample │ ├── Images.xcassets │ └── AppIcon.appiconset │ │ └── Contents.json │ ├── Info.plist │ ├── AppDelegate.swift │ ├── ViewController.swift │ └── Base.lproj │ ├── Main.storyboard │ └── LaunchScreen.xib ├── .gitignore ├── LICENSE ├── EMPickerView ├── EMPickerViewRow.swift ├── EMPickerViewProtocols.swift └── EMPickerView.swift └── README.md /EMPickerView_Date_Time.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/codeisjoy/EMPickerView/HEAD/EMPickerView_Date_Time.png -------------------------------------------------------------------------------- /Examples/EMPickerViewExample.xcodeproj/project.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Xcode 2 | # 3 | build/ 4 | *.pbxuser 5 | !default.pbxuser 6 | *.mode1v3 7 | !default.mode1v3 8 | *.mode2v3 9 | !default.mode2v3 10 | *.perspectivev3 11 | !default.perspectivev3 12 | xcuserdata 13 | *.xccheckout 14 | *.moved-aside 15 | DerivedData 16 | *.hmap 17 | *.ipa 18 | *.xcuserstate 19 | 20 | # CocoaPods 21 | # 22 | # We recommend against adding the Pods directory to your .gitignore. However 23 | # you should judge for yourself, the pros and cons are mentioned at: 24 | # http://guides.cocoapods.org/using/using-cocoapods.html#should-i-ignore-the-pods-directory-in-source-control 25 | # 26 | # Pods/ 27 | -------------------------------------------------------------------------------- /Examples/EMPickerViewExample/Images.xcassets/AppIcon.appiconset/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "images" : [ 3 | { 4 | "idiom" : "iphone", 5 | "size" : "29x29", 6 | "scale" : "2x" 7 | }, 8 | { 9 | "idiom" : "iphone", 10 | "size" : "29x29", 11 | "scale" : "3x" 12 | }, 13 | { 14 | "idiom" : "iphone", 15 | "size" : "40x40", 16 | "scale" : "2x" 17 | }, 18 | { 19 | "idiom" : "iphone", 20 | "size" : "40x40", 21 | "scale" : "3x" 22 | }, 23 | { 24 | "idiom" : "iphone", 25 | "size" : "60x60", 26 | "scale" : "2x" 27 | }, 28 | { 29 | "idiom" : "iphone", 30 | "size" : "60x60", 31 | "scale" : "3x" 32 | } 33 | ], 34 | "info" : { 35 | "version" : 1, 36 | "author" : "xcode" 37 | } 38 | } -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 Emad A. 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | 23 | -------------------------------------------------------------------------------- /Examples/EMPickerViewExample/Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | CFBundleDevelopmentRegion 6 | en 7 | CFBundleExecutable 8 | $(EXECUTABLE_NAME) 9 | CFBundleIdentifier 10 | com.codeisjoy.$(PRODUCT_NAME:rfc1034identifier) 11 | CFBundleInfoDictionaryVersion 12 | 6.0 13 | CFBundleName 14 | $(PRODUCT_NAME) 15 | CFBundlePackageType 16 | APPL 17 | CFBundleShortVersionString 18 | 1.0 19 | CFBundleSignature 20 | ???? 21 | CFBundleVersion 22 | 1 23 | LSRequiresIPhoneOS 24 | 25 | UILaunchStoryboardName 26 | LaunchScreen 27 | UIMainStoryboardFile 28 | Main 29 | UIRequiredDeviceCapabilities 30 | 31 | armv7 32 | 33 | UISupportedInterfaceOrientations 34 | 35 | UIInterfaceOrientationPortrait 36 | UIInterfaceOrientationLandscapeLeft 37 | UIInterfaceOrientationLandscapeRight 38 | 39 | 40 | 41 | -------------------------------------------------------------------------------- /EMPickerView/EMPickerViewRow.swift: -------------------------------------------------------------------------------- 1 | // 2 | // EMPickerViewRow.swift 3 | // EMPickerViewExample 4 | // 5 | // Created by Emad A. on 7/02/2015. 6 | // Copyright (c) 2015 Emad A. All rights reserved. 7 | // 8 | 9 | import UIKit 10 | 11 | class EMPickerViewRow: UITableViewCell { 12 | 13 | required init(coder aDecoder: NSCoder) { 14 | super.init(coder: aDecoder) 15 | initialize() 16 | } 17 | 18 | override init(style: UITableViewCellStyle, reuseIdentifier: String?) { 19 | super.init(style: style, reuseIdentifier: reuseIdentifier) 20 | initialize() 21 | } 22 | 23 | override func setSelected(selected: Bool, animated: Bool) { 24 | super.setSelected(selected, animated: animated) 25 | 26 | if let label = self.textLabel { 27 | let textLabelAlpha: CGFloat = selected ? 1.0 : 0.5 28 | UIView.animateWithDuration(0.25, animations: { label.alpha = textLabelAlpha }) 29 | } 30 | } 31 | 32 | override func layoutSubviews() { 33 | super.layoutSubviews() 34 | 35 | textLabel?.frame = textLabel?.superview?.bounds ?? CGRectZero 36 | } 37 | 38 | private func initialize() { 39 | selectionStyle = .None 40 | backgroundColor = UIColor.clearColor() 41 | if respondsToSelector(Selector("setLayoutMargins:")) { 42 | layoutMargins = UIEdgeInsetsZero 43 | } 44 | if respondsToSelector(Selector("setSeparatorInset:")) { 45 | separatorInset = UIEdgeInsetsZero 46 | } 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /Examples/EMPickerViewExample/AppDelegate.swift: -------------------------------------------------------------------------------- 1 | // 2 | // AppDelegate.swift 3 | // EMPickerViewExample 4 | // 5 | // Created by Emad A. on 4/02/2015. 6 | // Copyright (c) 2015 Emad A. All rights reserved. 7 | // 8 | 9 | import UIKit 10 | 11 | @UIApplicationMain 12 | class AppDelegate: UIResponder, UIApplicationDelegate { 13 | 14 | var window: UIWindow? 15 | 16 | 17 | func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { 18 | // Override point for customization after application launch. 19 | return true 20 | } 21 | 22 | func applicationWillResignActive(application: UIApplication) { 23 | // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state. 24 | // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game. 25 | } 26 | 27 | func applicationDidEnterBackground(application: UIApplication) { 28 | // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. 29 | // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits. 30 | } 31 | 32 | func applicationWillEnterForeground(application: UIApplication) { 33 | // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background. 34 | } 35 | 36 | func applicationDidBecomeActive(application: UIApplication) { 37 | // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface. 38 | } 39 | 40 | func applicationWillTerminate(application: UIApplication) { 41 | // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. 42 | } 43 | 44 | 45 | } 46 | 47 | -------------------------------------------------------------------------------- /Examples/EMPickerViewExample/ViewController.swift: -------------------------------------------------------------------------------- 1 | // 2 | // ViewController.swift 3 | // EMPickerViewExample 4 | // 5 | // Created by Emad A. on 4/02/2015. 6 | // Copyright (c) 2015 Emad A. All rights reserved. 7 | // 8 | 9 | import UIKit 10 | 11 | class ViewController: UIViewController { 12 | 13 | @IBOutlet var pv:EMPickerView? 14 | 15 | override func viewDidLoad() { 16 | super.viewDidLoad() 17 | 18 | pv?.delegate = self; 19 | pv?.dataSource = self; 20 | 21 | view.addSubview(pv!) 22 | 23 | pv?.selectRowAtIndex(3, component: 0, section: 0, animated: false) 24 | pv?.selectRowAtIndex(4, component: 1, section: 0, animated: false) 25 | pv?.selectRowAtIndex(1, component: 2, section: 0, animated: false) 26 | } 27 | 28 | override func didReceiveMemoryWarning() { 29 | super.didReceiveMemoryWarning() 30 | // Dispose of any resources that can be recreated. 31 | } 32 | } 33 | 34 | extension ViewController: EMPickerViewDataSource { 35 | 36 | func numberOfSectionsInPickerView(pickerView: EMPickerView) -> Int { 37 | return 3 38 | } 39 | 40 | func pickerView(pickerView: EMPickerView, numberOfComponentsInSection section: Int) -> Int { 41 | return section == 0 ? 3 : 1 42 | } 43 | 44 | func pickerView(pickerView: EMPickerView, numberOfRowsInComponent component: Int, section: Int) -> Int { 45 | return component > 1 ? 2 : 10 46 | } 47 | } 48 | 49 | extension ViewController: EMPickerViewDelegate { 50 | 51 | func pickerView(pickerView: EMPickerView, titleForRow row: Int, component: Int, section: Int) -> String { 52 | return "\(section):\(component):\(row)" 53 | } 54 | 55 | func pickerView(pickerView: EMPickerView, attributtedTitleForRow row: Int, component: Int, section: Int) -> NSAttributedString? { 56 | var text: NSMutableAttributedString = NSMutableAttributedString(string: "\(section):\(component):\(row)") 57 | text.addAttribute(NSFontAttributeName, value: UIFont(name: "Avenir-Medium", size: 16)!, range: NSMakeRange(0, text.length)) 58 | 59 | if (section == 0 && component == 1) { 60 | let paragraphStyle: NSMutableParagraphStyle = NSMutableParagraphStyle() 61 | paragraphStyle.alignment = .Left 62 | 63 | text.addAttribute(NSParagraphStyleAttributeName, value: paragraphStyle, range: NSMakeRange(0, text.length)) 64 | } 65 | 66 | return text 67 | } 68 | 69 | func widthFractionForSectionsInPickerView(pickerView: EMPickerView) -> [CGFloat] { 70 | return [0.6, 0.2, 0.2] 71 | } 72 | 73 | func pickerView(pickerView: EMPickerView, widthForComponentsInSection section: Int) -> [CGFloat]? { 74 | if section == 0 { 75 | return [0.3, 0.4, 0.3] 76 | } 77 | 78 | return nil 79 | } 80 | 81 | func pickerView(pickerView: EMPickerView, didSelectRowAtIndex row: Int, component: Int, section: Int) { 82 | println("section:\(section) :: component:\(component) :: row:\(pickerView.selectedRow(component: component, section: section))") 83 | } 84 | } -------------------------------------------------------------------------------- /EMPickerView/EMPickerViewProtocols.swift: -------------------------------------------------------------------------------- 1 | // 2 | // EMPickerViewDataSource.swift 3 | // EMPickerViewExample 4 | // 5 | // Created by Emad A. on 4/02/2015. 6 | // Copyright (c) 2015 Emad A. All rights reserved. 7 | // 8 | 9 | import UIKit 10 | import Foundation 11 | 12 | // MARK: - EMPickerViewDataSource 13 | // MARK: - 14 | 15 | @objc protocol EMPickerViewDataSource { 16 | 17 | // Called by the picker view when it needs the number of section. (optional) 18 | // If it is not defined default value 1 will be returned. 19 | optional func numberOfSectionsInPickerView(pickerView: EMPickerView) -> Int 20 | 21 | // Called by the picker view when it needs the number of components for a specific section. (required) 22 | func pickerView(pickerView: EMPickerView, numberOfComponentsInSection section: Int) -> Int 23 | 24 | // Called by the picker view when it needs the number of rows for a specific component of a specific section. (required) 25 | func pickerView(pickerView: EMPickerView, numberOfRowsInComponent component: Int, section: Int) -> Int 26 | } 27 | 28 | // MARK: - EMPickerViewDelegate 29 | // MARK: - 30 | 31 | @objc protocol EMPickerViewDelegate { 32 | 33 | // Called by the picker view when it needs the section width to use for drawing the content. 34 | // You should return an array of CGFloat between 0 and 1. Each number represents the section width portion at that index. 35 | // If this method is not implemented all sections will have the same width through the picker view. 36 | optional func widthFractionForSectionsInPickerView(pickerView: EMPickerView) -> [CGFloat] 37 | 38 | // Called by the picker view when it needs the components width to use for drawing the content. 39 | // You should return an array of CGFloat between 0 and 1. Each number represents the section width portion at that index. 40 | // If this method is not implemented all components will have the same width through its section. 41 | optional func pickerView(pickerView: EMPickerView, widthFractionForComponentsInSection section: Int) -> [CGFloat]? 42 | 43 | // Called by the picker view when it needs the title to use for a given row in a given component and section. 44 | func pickerView(pickerView: EMPickerView, titleForRow row: Int, component: Int, section: Int) -> String 45 | 46 | // Called by the picker view when it needs the styled title to use for a given row in a given component. 47 | // If you implement both this method and the pickerView:titleForRow:component:section method, the picker view prefers the use of this method. 48 | // However, if your implementation of this method returns nil, the picker view falls back to using the string returned by 49 | // the pickerView:titleForRow:component:section method. 50 | optional func pickerView(pickerView: EMPickerView, attributtedTitleForRow row: Int, component: Int, section: Int) -> NSAttributedString? 51 | 52 | // Called by the picker view when the user selects a row in a component and section and gives their index values. 53 | optional func pickerView(pickerView: EMPickerView, didSelectRowAtIndex row: Int, component: Int, section: Int) 54 | 55 | } -------------------------------------------------------------------------------- /Examples/EMPickerViewExample/Base.lproj/Main.storyboard: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | -------------------------------------------------------------------------------- /Examples/EMPickerViewExample/Base.lproj/LaunchScreen.xib: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 20 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # EMPickerView 2 | A customisable picker view in Swift 3 | 4 | ![EMPickerView Screenshot](https://github.com/codeisjoy/EMPickerView/blob/master/EMPickerView_Date_Time.png) 5 | 6 | The user interface provided by a picker view consists of sections, components and rows. 7 | * A section is a set of components which have the same indicator lines. 8 | Each section has an indexed location (left to right) in the picke view. 9 | * A component is a wheel, which has a series of items (rows) at indexed locations on the wheel. 10 | Each component, same as section, has an indexed location (left to right) in its section. 11 | Each row on a component could have a label, which is a simple or attributed string. 12 | 13 | ## Usage 14 | It is a subclass of UIView so could be initialized either in Storyboard or by code. 15 | 16 | let pickerView: EMPickerView = EMPickerView(frame: <#CGRect#>) 17 | 18 | Then set the `dataSource` and `delegate` properties. 19 | The methods of `EMPickerViewDataSource` and `EMPickerViewDelegate` will control how the picker view looks like. 20 | 21 | #### Properties 22 | 23 | - `componentsRowHeight`
24 | A float value indicating the height of the component rows 25 |
26 | - `tintColor`
27 | The tint colour to apply to the selection indicator lines 28 |
29 | - `dataSource`
30 | The data source for the picker view. 31 | 32 | #### Methods 33 | 34 | - `func selectRowAtIndex(index: Int, component: Int, section: Int, animated: Bool)`
35 | Selects a specific row and given component and section index. 36 |
37 | - `func selectedRow(#component: Int, section: Int) -> Int`
38 | Gives the selected row index for given component and section index. 39 | 40 | #### The data source `EMPickerViewDataSource`: 41 | 42 | - `func pickerView(pickerView: EMPickerView, numberOfComponentsInSection section: Int) -> Int`
43 | Called by the picker view when it needs the number of components for a specific section. (required) 44 |
45 | - `func pickerView(pickerView: EMPickerView, numberOfRowsInComponent component: Int, section: Int) -> Int`
46 | Called by the picker view when it needs the number of rows for a specific component of a specific section. (required) 47 |
48 | - `optional func numberOfSectionsInPickerView(pickerView: EMPickerView) -> Int`
49 | Called by the picker view when it needs the number of section. (optional)
50 | If it is not defined default value 1 will be returned. 51 | 52 | #### The delegate `EMPickerViewDelegate`: 53 | 54 | - `optional func widthFractionForSectionsInPickerView(pickerView: EMPickerView) -> [CGFloat]`
55 | Called by the picker view when it needs the section width to use for drawing the content.
56 | You should return an array of `CGFloat` between `0` and `1`. Each number represents the section width portion at that index.
57 | If this method is not implemented all sections will have the same width through the picker view. 58 |
59 | - `optional func pickerView(pickerView: EMPickerView, widthFractionForComponentsInSection section: Int) -> [CGFloat]?`
60 | Called by the picker view when it needs the components width to use for drawing the content.
61 | You should return an array of `CGFloat` between `0` and `1`. Each number represents the section width portion at that index. 62 | If this method is not implemented all components will have the same width through its section. 63 |
64 | - `func pickerView(pickerView: EMPickerView, titleForRow row: Int, component: Int, section: Int) -> String`
65 | Called by the picker view when it needs the title to use for a given row in a given component and section. 66 |
67 | - `optional func pickerView(pickerView: EMPickerView, attributtedTitleForRow row: Int, component: Int, section: Int) -> NSAttributedString?`
68 | Called by the picker view when it needs the styled title to use for a given row in a given component.
69 | If you implement both this method and the `pickerView:titleForRow:component:section` method, the picker view prefers the use of this method. 70 | However, if your implementation of this method returns nil, the picker view falls back to using the string returned by the pickerView:titleForRow:component:section method. 71 |
72 | - `optional func pickerView(pickerView: EMPickerView, didSelectRowAtIndex row: Int, component: Int, section: Int)`
73 | Called by the picker view when the user selects a row in a component and section and gives their index values. 74 | 75 | ## Install 76 | Simply add it as a submodule then import `EMPickerView` folder into your Xcode project. 77 | 78 | git submodule add https://github.com/codeisjoy/EMPickerView.git 79 | 80 | ## Note 81 | This is the basic picker view. To have a date/time picker view the only thing that should be done is providing your date and time data through the `dataSource` methods and modifying the `delegate` methods to have your own picker view set. 82 | -------------------------------------------------------------------------------- /Examples/EMPickerViewExample.xcodeproj/project.pbxproj: -------------------------------------------------------------------------------- 1 | // !$*UTF8*$! 2 | { 3 | archiveVersion = 1; 4 | classes = { 5 | }; 6 | objectVersion = 46; 7 | objects = { 8 | 9 | /* Begin PBXBuildFile section */ 10 | 0415C5691A8185EC0075CF80 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0415C5681A8185EC0075CF80 /* AppDelegate.swift */; }; 11 | 0415C56B1A8185EC0075CF80 /* ViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0415C56A1A8185EC0075CF80 /* ViewController.swift */; }; 12 | 0415C56E1A8185ED0075CF80 /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 0415C56C1A8185EC0075CF80 /* Main.storyboard */; }; 13 | 0415C5701A8185ED0075CF80 /* Images.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 0415C56F1A8185ED0075CF80 /* Images.xcassets */; }; 14 | 0415C5731A8185ED0075CF80 /* LaunchScreen.xib in Resources */ = {isa = PBXBuildFile; fileRef = 0415C5711A8185ED0075CF80 /* LaunchScreen.xib */; }; 15 | 0415C58A1A8186530075CF80 /* EMPickerView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0415C5891A8186530075CF80 /* EMPickerView.swift */; }; 16 | 0415C58C1A81885D0075CF80 /* EMPickerViewProtocols.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0415C58B1A81885D0075CF80 /* EMPickerViewProtocols.swift */; }; 17 | 045EA1BB1A8608BC007B5598 /* EMPickerViewRow.swift in Sources */ = {isa = PBXBuildFile; fileRef = 045EA1BA1A8608BC007B5598 /* EMPickerViewRow.swift */; }; 18 | /* End PBXBuildFile section */ 19 | 20 | /* Begin PBXFileReference section */ 21 | 0415C5631A8185EC0075CF80 /* EMPickerViewExample.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = EMPickerViewExample.app; sourceTree = BUILT_PRODUCTS_DIR; }; 22 | 0415C5671A8185EC0075CF80 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; 23 | 0415C5681A8185EC0075CF80 /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = ""; }; 24 | 0415C56A1A8185EC0075CF80 /* ViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ViewController.swift; sourceTree = ""; }; 25 | 0415C56D1A8185ED0075CF80 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/Main.storyboard; sourceTree = ""; }; 26 | 0415C56F1A8185ED0075CF80 /* Images.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Images.xcassets; sourceTree = ""; }; 27 | 0415C5721A8185ED0075CF80 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.xib; name = Base; path = Base.lproj/LaunchScreen.xib; sourceTree = ""; }; 28 | 0415C5891A8186530075CF80 /* EMPickerView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = EMPickerView.swift; path = ../EMPickerView/EMPickerView.swift; sourceTree = ""; }; 29 | 0415C58B1A81885D0075CF80 /* EMPickerViewProtocols.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = EMPickerViewProtocols.swift; path = ../EMPickerView/EMPickerViewProtocols.swift; sourceTree = ""; }; 30 | 045EA1BA1A8608BC007B5598 /* EMPickerViewRow.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = EMPickerViewRow.swift; path = ../EMPickerView/EMPickerViewRow.swift; sourceTree = ""; }; 31 | /* End PBXFileReference section */ 32 | 33 | /* Begin PBXFrameworksBuildPhase section */ 34 | 0415C5601A8185EC0075CF80 /* Frameworks */ = { 35 | isa = PBXFrameworksBuildPhase; 36 | buildActionMask = 2147483647; 37 | files = ( 38 | ); 39 | runOnlyForDeploymentPostprocessing = 0; 40 | }; 41 | /* End PBXFrameworksBuildPhase section */ 42 | 43 | /* Begin PBXGroup section */ 44 | 0415C55A1A8185EC0075CF80 = { 45 | isa = PBXGroup; 46 | children = ( 47 | 0415C5881A8186370075CF80 /* EMPickerView */, 48 | 0415C5651A8185EC0075CF80 /* EMPickerViewExample */, 49 | 0415C5641A8185EC0075CF80 /* Products */, 50 | ); 51 | sourceTree = ""; 52 | }; 53 | 0415C5641A8185EC0075CF80 /* Products */ = { 54 | isa = PBXGroup; 55 | children = ( 56 | 0415C5631A8185EC0075CF80 /* EMPickerViewExample.app */, 57 | ); 58 | name = Products; 59 | sourceTree = ""; 60 | }; 61 | 0415C5651A8185EC0075CF80 /* EMPickerViewExample */ = { 62 | isa = PBXGroup; 63 | children = ( 64 | 0415C5681A8185EC0075CF80 /* AppDelegate.swift */, 65 | 0415C56A1A8185EC0075CF80 /* ViewController.swift */, 66 | 0415C56C1A8185EC0075CF80 /* Main.storyboard */, 67 | 0415C56F1A8185ED0075CF80 /* Images.xcassets */, 68 | 0415C5711A8185ED0075CF80 /* LaunchScreen.xib */, 69 | 0415C5661A8185EC0075CF80 /* Supporting Files */, 70 | ); 71 | path = EMPickerViewExample; 72 | sourceTree = ""; 73 | }; 74 | 0415C5661A8185EC0075CF80 /* Supporting Files */ = { 75 | isa = PBXGroup; 76 | children = ( 77 | 0415C5671A8185EC0075CF80 /* Info.plist */, 78 | ); 79 | name = "Supporting Files"; 80 | sourceTree = ""; 81 | }; 82 | 0415C5881A8186370075CF80 /* EMPickerView */ = { 83 | isa = PBXGroup; 84 | children = ( 85 | 0415C5891A8186530075CF80 /* EMPickerView.swift */, 86 | 045EA1BA1A8608BC007B5598 /* EMPickerViewRow.swift */, 87 | 0415C58B1A81885D0075CF80 /* EMPickerViewProtocols.swift */, 88 | ); 89 | name = EMPickerView; 90 | sourceTree = ""; 91 | }; 92 | /* End PBXGroup section */ 93 | 94 | /* Begin PBXNativeTarget section */ 95 | 0415C5621A8185EC0075CF80 /* EMPickerViewExample */ = { 96 | isa = PBXNativeTarget; 97 | buildConfigurationList = 0415C5821A8185ED0075CF80 /* Build configuration list for PBXNativeTarget "EMPickerViewExample" */; 98 | buildPhases = ( 99 | 0415C55F1A8185EC0075CF80 /* Sources */, 100 | 0415C5601A8185EC0075CF80 /* Frameworks */, 101 | 0415C5611A8185EC0075CF80 /* Resources */, 102 | ); 103 | buildRules = ( 104 | ); 105 | dependencies = ( 106 | ); 107 | name = EMPickerViewExample; 108 | productName = EMPickerViewExample; 109 | productReference = 0415C5631A8185EC0075CF80 /* EMPickerViewExample.app */; 110 | productType = "com.apple.product-type.application"; 111 | }; 112 | /* End PBXNativeTarget section */ 113 | 114 | /* Begin PBXProject section */ 115 | 0415C55B1A8185EC0075CF80 /* Project object */ = { 116 | isa = PBXProject; 117 | attributes = { 118 | LastUpgradeCheck = 0610; 119 | ORGANIZATIONNAME = "Emad A."; 120 | TargetAttributes = { 121 | 0415C5621A8185EC0075CF80 = { 122 | CreatedOnToolsVersion = 6.1.1; 123 | }; 124 | }; 125 | }; 126 | buildConfigurationList = 0415C55E1A8185EC0075CF80 /* Build configuration list for PBXProject "EMPickerViewExample" */; 127 | compatibilityVersion = "Xcode 3.2"; 128 | developmentRegion = English; 129 | hasScannedForEncodings = 0; 130 | knownRegions = ( 131 | en, 132 | Base, 133 | ); 134 | mainGroup = 0415C55A1A8185EC0075CF80; 135 | productRefGroup = 0415C5641A8185EC0075CF80 /* Products */; 136 | projectDirPath = ""; 137 | projectRoot = ""; 138 | targets = ( 139 | 0415C5621A8185EC0075CF80 /* EMPickerViewExample */, 140 | ); 141 | }; 142 | /* End PBXProject section */ 143 | 144 | /* Begin PBXResourcesBuildPhase section */ 145 | 0415C5611A8185EC0075CF80 /* Resources */ = { 146 | isa = PBXResourcesBuildPhase; 147 | buildActionMask = 2147483647; 148 | files = ( 149 | 0415C56E1A8185ED0075CF80 /* Main.storyboard in Resources */, 150 | 0415C5731A8185ED0075CF80 /* LaunchScreen.xib in Resources */, 151 | 0415C5701A8185ED0075CF80 /* Images.xcassets in Resources */, 152 | ); 153 | runOnlyForDeploymentPostprocessing = 0; 154 | }; 155 | /* End PBXResourcesBuildPhase section */ 156 | 157 | /* Begin PBXSourcesBuildPhase section */ 158 | 0415C55F1A8185EC0075CF80 /* Sources */ = { 159 | isa = PBXSourcesBuildPhase; 160 | buildActionMask = 2147483647; 161 | files = ( 162 | 0415C56B1A8185EC0075CF80 /* ViewController.swift in Sources */, 163 | 0415C58C1A81885D0075CF80 /* EMPickerViewProtocols.swift in Sources */, 164 | 045EA1BB1A8608BC007B5598 /* EMPickerViewRow.swift in Sources */, 165 | 0415C58A1A8186530075CF80 /* EMPickerView.swift in Sources */, 166 | 0415C5691A8185EC0075CF80 /* AppDelegate.swift in Sources */, 167 | ); 168 | runOnlyForDeploymentPostprocessing = 0; 169 | }; 170 | /* End PBXSourcesBuildPhase section */ 171 | 172 | /* Begin PBXVariantGroup section */ 173 | 0415C56C1A8185EC0075CF80 /* Main.storyboard */ = { 174 | isa = PBXVariantGroup; 175 | children = ( 176 | 0415C56D1A8185ED0075CF80 /* Base */, 177 | ); 178 | name = Main.storyboard; 179 | sourceTree = ""; 180 | }; 181 | 0415C5711A8185ED0075CF80 /* LaunchScreen.xib */ = { 182 | isa = PBXVariantGroup; 183 | children = ( 184 | 0415C5721A8185ED0075CF80 /* Base */, 185 | ); 186 | name = LaunchScreen.xib; 187 | sourceTree = ""; 188 | }; 189 | /* End PBXVariantGroup section */ 190 | 191 | /* Begin XCBuildConfiguration section */ 192 | 0415C5801A8185ED0075CF80 /* Debug */ = { 193 | isa = XCBuildConfiguration; 194 | buildSettings = { 195 | ALWAYS_SEARCH_USER_PATHS = NO; 196 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; 197 | CLANG_CXX_LIBRARY = "libc++"; 198 | CLANG_ENABLE_MODULES = YES; 199 | CLANG_ENABLE_OBJC_ARC = YES; 200 | CLANG_WARN_BOOL_CONVERSION = YES; 201 | CLANG_WARN_CONSTANT_CONVERSION = YES; 202 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 203 | CLANG_WARN_EMPTY_BODY = YES; 204 | CLANG_WARN_ENUM_CONVERSION = YES; 205 | CLANG_WARN_INT_CONVERSION = YES; 206 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 207 | CLANG_WARN_UNREACHABLE_CODE = YES; 208 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 209 | "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; 210 | COPY_PHASE_STRIP = NO; 211 | ENABLE_STRICT_OBJC_MSGSEND = YES; 212 | GCC_C_LANGUAGE_STANDARD = gnu99; 213 | GCC_DYNAMIC_NO_PIC = NO; 214 | GCC_OPTIMIZATION_LEVEL = 0; 215 | GCC_PREPROCESSOR_DEFINITIONS = ( 216 | "DEBUG=1", 217 | "$(inherited)", 218 | ); 219 | GCC_SYMBOLS_PRIVATE_EXTERN = NO; 220 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 221 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 222 | GCC_WARN_UNDECLARED_SELECTOR = YES; 223 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 224 | GCC_WARN_UNUSED_FUNCTION = YES; 225 | GCC_WARN_UNUSED_VARIABLE = YES; 226 | IPHONEOS_DEPLOYMENT_TARGET = 8.1; 227 | MTL_ENABLE_DEBUG_INFO = YES; 228 | ONLY_ACTIVE_ARCH = YES; 229 | SDKROOT = iphoneos; 230 | SWIFT_OPTIMIZATION_LEVEL = "-Onone"; 231 | }; 232 | name = Debug; 233 | }; 234 | 0415C5811A8185ED0075CF80 /* Release */ = { 235 | isa = XCBuildConfiguration; 236 | buildSettings = { 237 | ALWAYS_SEARCH_USER_PATHS = NO; 238 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; 239 | CLANG_CXX_LIBRARY = "libc++"; 240 | CLANG_ENABLE_MODULES = YES; 241 | CLANG_ENABLE_OBJC_ARC = YES; 242 | CLANG_WARN_BOOL_CONVERSION = YES; 243 | CLANG_WARN_CONSTANT_CONVERSION = YES; 244 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 245 | CLANG_WARN_EMPTY_BODY = YES; 246 | CLANG_WARN_ENUM_CONVERSION = YES; 247 | CLANG_WARN_INT_CONVERSION = YES; 248 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 249 | CLANG_WARN_UNREACHABLE_CODE = YES; 250 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 251 | "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; 252 | COPY_PHASE_STRIP = YES; 253 | ENABLE_NS_ASSERTIONS = NO; 254 | ENABLE_STRICT_OBJC_MSGSEND = YES; 255 | GCC_C_LANGUAGE_STANDARD = gnu99; 256 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 257 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 258 | GCC_WARN_UNDECLARED_SELECTOR = YES; 259 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 260 | GCC_WARN_UNUSED_FUNCTION = YES; 261 | GCC_WARN_UNUSED_VARIABLE = YES; 262 | IPHONEOS_DEPLOYMENT_TARGET = 8.1; 263 | MTL_ENABLE_DEBUG_INFO = NO; 264 | SDKROOT = iphoneos; 265 | VALIDATE_PRODUCT = YES; 266 | }; 267 | name = Release; 268 | }; 269 | 0415C5831A8185ED0075CF80 /* Debug */ = { 270 | isa = XCBuildConfiguration; 271 | buildSettings = { 272 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; 273 | INFOPLIST_FILE = EMPickerViewExample/Info.plist; 274 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; 275 | PRODUCT_NAME = "$(TARGET_NAME)"; 276 | }; 277 | name = Debug; 278 | }; 279 | 0415C5841A8185ED0075CF80 /* Release */ = { 280 | isa = XCBuildConfiguration; 281 | buildSettings = { 282 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; 283 | INFOPLIST_FILE = EMPickerViewExample/Info.plist; 284 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; 285 | PRODUCT_NAME = "$(TARGET_NAME)"; 286 | }; 287 | name = Release; 288 | }; 289 | /* End XCBuildConfiguration section */ 290 | 291 | /* Begin XCConfigurationList section */ 292 | 0415C55E1A8185EC0075CF80 /* Build configuration list for PBXProject "EMPickerViewExample" */ = { 293 | isa = XCConfigurationList; 294 | buildConfigurations = ( 295 | 0415C5801A8185ED0075CF80 /* Debug */, 296 | 0415C5811A8185ED0075CF80 /* Release */, 297 | ); 298 | defaultConfigurationIsVisible = 0; 299 | defaultConfigurationName = Release; 300 | }; 301 | 0415C5821A8185ED0075CF80 /* Build configuration list for PBXNativeTarget "EMPickerViewExample" */ = { 302 | isa = XCConfigurationList; 303 | buildConfigurations = ( 304 | 0415C5831A8185ED0075CF80 /* Debug */, 305 | 0415C5841A8185ED0075CF80 /* Release */, 306 | ); 307 | defaultConfigurationIsVisible = 0; 308 | defaultConfigurationName = Release; 309 | }; 310 | /* End XCConfigurationList section */ 311 | }; 312 | rootObject = 0415C55B1A8185EC0075CF80 /* Project object */; 313 | } 314 | -------------------------------------------------------------------------------- /EMPickerView/EMPickerView.swift: -------------------------------------------------------------------------------- 1 | // 2 | // EMPickerView.swift 3 | // EMPickerViewExample 4 | // 5 | // Created by Emad A. on 4/02/2015. 6 | // Copyright (c) 2015 Emad A. All rights reserved. 7 | // 8 | 9 | import UIKit 10 | 11 | // MARK: - EMPickerView 12 | // MARK: - 13 | 14 | class EMPickerView: UIView { 15 | 16 | // MARK: - Constants 17 | 18 | private let sectionsGap: CGFloat = 8 19 | private let componentRowId: String = "EMPickerViewComponentRow" 20 | private let defaultTintColor: UIColor = UIColor(red:0.28, green:0.61, blue:1, alpha:1) 21 | 22 | // MARK: - Public Properties 23 | 24 | var componentsRowHeight: CGFloat = 50 25 | 26 | @IBOutlet var delegate: EMPickerViewDelegate? 27 | @IBOutlet var dataSource: EMPickerViewDataSource? { 28 | didSet { 29 | setNeedsValidateDataSource() 30 | } 31 | } 32 | 33 | override var tintColor: UIColor! { 34 | didSet { 35 | for s in 0...sections.count { 36 | if let inds = indicators[s] { 37 | inds.0.backgroundColor = self.tintColor 38 | inds.1.backgroundColor = self.tintColor 39 | } 40 | } 41 | } 42 | } 43 | 44 | // MARK: - Private Properties 45 | 46 | private var sections = [UIView]() 47 | private var components = [Int: [UITableView]]() 48 | private var indicators = [Int: (UIView, UIView)]() 49 | 50 | // MARK: - Overriden Methods 51 | 52 | required init(coder aDecoder: NSCoder) { 53 | super.init(coder: aDecoder) 54 | tintColor = defaultTintColor 55 | } 56 | 57 | override init(frame: CGRect) { 58 | super.init(frame: frame) 59 | tintColor = defaultTintColor 60 | } 61 | 62 | override func layoutSubviews() { 63 | super.layoutSubviews() 64 | 65 | // Setting the position of sections based on their index 66 | 67 | let uprightWidth: CGFloat = bounds.width - CGFloat(sections.count - 1) * sectionsGap 68 | 69 | var size: CGSize = CGSize(width: 0, height: bounds.height) 70 | let sws: [CGFloat]? = delegate?.widthFractionForSectionsInPickerView?(self) 71 | if sws == nil { 72 | size.width = ceil(uprightWidth / CGFloat(sections.count)) 73 | } 74 | else { 75 | assert(sws!.count == sections.count, "The width of sections has been set improperly.") 76 | 77 | let ws = sws!.reduce(0) { $0 + $1 } 78 | assert(ws == 1, "The width of sections has been set improperly.") 79 | } 80 | 81 | for (s, section) in enumerate(sections) { 82 | var origin: CGPoint = CGPointMake(0, CGRectGetMinY(bounds)) 83 | if sws != nil { 84 | size.width = uprightWidth * sws![s] 85 | if s == 0 { 86 | origin.x = CGRectGetMinX(bounds) 87 | } 88 | else { 89 | let previous = sections[s - 1] 90 | origin.x = CGRectGetMaxX(previous.frame) + sectionsGap 91 | } 92 | } 93 | else { 94 | origin.x = CGFloat(s) * (size.width + sectionsGap) 95 | } 96 | section.frame = CGRect(origin: origin, size: size) 97 | 98 | // Setting the position of components of this section based on their index 99 | if let componentsForSection = components[s] { 100 | var size: CGSize = CGSizeMake(0, section.bounds.height) 101 | let cws: [CGFloat]? = delegate?.pickerView?(self, widthFractionForComponentsInSection: s) 102 | if cws == nil { 103 | size.width = ceil(section.bounds.width / CGFloat(componentsForSection.count)) 104 | } 105 | else { 106 | assert(cws!.count == componentsForSection.count, "The width of components for section \(s) has been set improperly.") 107 | 108 | let ws = cws!.reduce(0) { $0 + $1 } 109 | assert(ws == 1, "The width of components for section \(s) has been set improperly.") 110 | } 111 | 112 | // Going through components of this section ... 113 | for (c, component) in enumerate(componentsForSection) { 114 | var origin: CGPoint = CGPointMake(CGFloat(c) * size.width, CGRectGetMinY(section.bounds)) 115 | if cws != nil { 116 | size.width = section.bounds.width * cws![c] 117 | if c == 0 { 118 | origin.x = CGRectGetMinX(section.bounds) 119 | } 120 | else { 121 | let previous = componentsForSection[c - 1] 122 | origin.x = CGRectGetMaxX(previous.frame) 123 | } 124 | } 125 | component.frame = CGRect(origin: origin, size: size) 126 | component.contentInset = UIEdgeInsetsMake( 127 | (section.bounds.height - componentsRowHeight) / 2, 0, 128 | (section.bounds.height - componentsRowHeight) / 2, 0); 129 | 130 | var r: Int = 0; 131 | if let selectedIndexPath = component.indexPathForSelectedRow() { 132 | r = selectedIndexPath.row; 133 | } 134 | selectRowAtIndex(r, component: c, section: s, animated: false); 135 | } 136 | } 137 | 138 | // Setting the position of indicators of this section based on their index 139 | // Index 0 is for top and 1 is for bottom. 140 | if let indicatorsForSection = indicators[s] { 141 | indicatorsForSection.0.frame = CGRectMake( 142 | CGRectGetMinX(section.bounds), 143 | section.center.y - componentsRowHeight / 2, 144 | section.bounds.width, 145 | 2) 146 | indicatorsForSection.1.frame = CGRectMake( 147 | CGRectGetMinX(section.bounds), 148 | section.center.y + componentsRowHeight / 2 - 2, 149 | section.bounds.width, 150 | 2) 151 | } 152 | } 153 | } 154 | 155 | // MARK: - Public Methods 156 | 157 | func selectRowAtIndex(index: Int, component: Int, section: Int, animated: Bool) { 158 | let componentsAtSection: [UIView]? = components[section] 159 | if componentsAtSection == nil || componentsAtSection?.count < 1 { 160 | return 161 | } 162 | 163 | if let tableView = componentsAtSection![component] as? UITableView { 164 | tableView.selectRowAtIndexPath(NSIndexPath(forRow: index, inSection: 0), animated: animated, scrollPosition: .None) 165 | tableView.scrollToNearestSelectedRowAtScrollPosition(.None, animated: animated) 166 | } 167 | 168 | self.delegate?.pickerView?(self, didSelectRowAtIndex: index, component: component, section: section) 169 | } 170 | 171 | func selectedRow(#component: Int, section: Int) -> Int { 172 | if let componentsOfSection = components[section] where componentsOfSection.count > component { 173 | let componentView: UITableView = componentsOfSection[component] 174 | return componentView.indexPathForSelectedRow()?.row ?? NSNotFound 175 | } 176 | 177 | return NSNotFound 178 | } 179 | 180 | // MARK: - Private Methods 181 | 182 | private func setNeedsValidateDataSource() { 183 | // Deallocating current sections view 184 | for sec: UIView in sections { 185 | sec.removeFromSuperview() 186 | } 187 | sections.removeAll(keepCapacity: false) 188 | 189 | // Initializing new sections and keeping a refrence of them for further uses 190 | let numberOfSections: Int = dataSource?.numberOfSectionsInPickerView?(self) ?? 1 191 | for var index: Int = 0; index < numberOfSections; index++ { 192 | let section: UIView = UIView() 193 | section.clipsToBounds = true 194 | 195 | addSubview(section) 196 | sections.append(section) 197 | 198 | initComponents(forSection: index) 199 | initIndicators(forSection: index) 200 | } 201 | } 202 | 203 | private func initComponents(forSection section: Int) { 204 | // Deallocating current components view for each section 205 | if let componentsForSection = components[section] { 206 | for comp: UIView in componentsForSection { 207 | comp.removeFromSuperview() 208 | } 209 | components.removeValueForKey(section) 210 | } 211 | 212 | // Initializing new components for given section index and keeping a reference of them for further uses 213 | var componentsForSection = [UITableView]() 214 | let sectionView: UIView = sections[section] 215 | 216 | let numberOfComponents: Int = dataSource?.pickerView(self, numberOfComponentsInSection: section) ?? 0 217 | for var index: Int = 0; index < numberOfComponents; index++ { 218 | let component: UITableView = UITableView() 219 | component.registerClass(EMPickerViewRow.self, forCellReuseIdentifier: componentRowId) 220 | component.showsHorizontalScrollIndicator = false 221 | component.showsVerticalScrollIndicator = false 222 | component.backgroundColor = UIColor.clearColor() 223 | component.separatorStyle = .None 224 | component.dataSource = self 225 | component.delegate = self 226 | 227 | if component.respondsToSelector(Selector("setLayoutMargins:")) { 228 | component.layoutMargins = UIEdgeInsetsZero 229 | } 230 | if component.respondsToSelector(Selector("setSeparatorInset:")) { 231 | component.separatorInset = UIEdgeInsetsZero 232 | } 233 | 234 | sectionView.addSubview(component) 235 | componentsForSection.append(component) 236 | } 237 | components[section] = componentsForSection 238 | } 239 | 240 | private func initIndicators(forSection section: Int) { 241 | // Deallocating current indicators view for each section 242 | if let indicatorsForSection = indicators[section] { 243 | indicatorsForSection.0.removeFromSuperview() 244 | indicatorsForSection.1.removeFromSuperview() 245 | indicators.removeValueForKey(section) 246 | } 247 | 248 | // Initializing new indicators for given section index and keeping a reference of them for further uses 249 | var indicatorsForSection = (UIView(), UIView()) 250 | let sectionView: UIView = sections[section] 251 | 252 | indicatorsForSection.0.backgroundColor = tintColor 253 | sectionView.addSubview(indicatorsForSection.0) 254 | 255 | indicatorsForSection.1.backgroundColor = tintColor 256 | sectionView.addSubview(indicatorsForSection.1) 257 | 258 | indicators[section] = indicatorsForSection 259 | } 260 | 261 | private func scrollComponent(component: UITableView, toContentOffset offset: CGPoint, animated: Bool) { 262 | var point: CGPoint = offset 263 | let frame:CGRect = CGRectMake(0, floor((component.bounds.height - componentsRowHeight) / 2), component.bounds.width, componentsRowHeight); 264 | point.y = round((point.y + frame.origin.y) / frame.size.height) * frame.size.height; 265 | 266 | // Scrolling the component to proper place in order to bring the selection at center 267 | component.setContentOffset(CGPointMake(0, point.y - frame.origin.y), animated: animated) 268 | 269 | let componentIndexPath = indexForComponentView(component) 270 | let rowIndexPath: NSIndexPath? = component.indexPathForRowAtPoint(point) 271 | if rowIndexPath != nil && componentIndexPath.component != NSNotFound && componentIndexPath.section != NSNotFound { 272 | component.selectRowAtIndexPath(rowIndexPath, animated: false, scrollPosition: .None) 273 | delegate?.pickerView?(self, didSelectRowAtIndex: rowIndexPath!.row, component: componentIndexPath.component, section: componentIndexPath.section) 274 | } 275 | } 276 | 277 | private func indexForComponentView(component: UITableView) -> (component: Int, section: Int) { 278 | var componentIndex: Int = NSNotFound 279 | var sectionIndex: Int = NSNotFound 280 | var stop: Bool = false 281 | 282 | for (s, cms) in components { 283 | for (c, cm) in enumerate(cms) { 284 | if component.isEqual(cm) { 285 | componentIndex = c 286 | sectionIndex = s 287 | stop = true 288 | break 289 | } 290 | } 291 | if stop { break } 292 | } 293 | 294 | return (componentIndex, sectionIndex) 295 | } 296 | } 297 | 298 | // MARK: - UITableViewDataSource Methods 299 | // MARK: - 300 | 301 | extension EMPickerView: UITableViewDataSource { 302 | 303 | func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 304 | let index = indexForComponentView(tableView) 305 | if dataSource != nil { 306 | return dataSource!.pickerView(self, numberOfRowsInComponent: index.component, section: index.section) 307 | } 308 | return 0 309 | } 310 | 311 | func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 312 | var cell: UITableViewCell = tableView.dequeueReusableCellWithIdentifier(componentRowId) as! UITableViewCell 313 | cell.textLabel?.textAlignment = .Center 314 | 315 | let index = indexForComponentView(tableView) 316 | if index.component != NSNotFound && index.section != NSNotFound { 317 | if let text = delegate?.pickerView?(self, attributtedTitleForRow: indexPath.row, component: index.component, section: index.section) { 318 | cell.textLabel?.attributedText = text 319 | } 320 | else { 321 | cell.textLabel?.text = delegate?.pickerView(self, titleForRow: indexPath.row, component: index.component, section: index.section) 322 | } 323 | } 324 | 325 | return cell 326 | } 327 | } 328 | 329 | // MARK: - UITableViewDelegate Methods 330 | // MARK: - 331 | 332 | extension EMPickerView: UITableViewDelegate { 333 | 334 | func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat { 335 | return componentsRowHeight 336 | } 337 | 338 | func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { 339 | let index = indexForComponentView(tableView) 340 | if index.component != NSNotFound && index.section != NSNotFound { 341 | selectRowAtIndex(indexPath.row, component: index.component, section: index.section, animated: true) 342 | } 343 | } 344 | } 345 | 346 | // MARK: - UIScrollViewDelegate Methods 347 | // MARK: - 348 | 349 | extension EMPickerView: UIScrollViewDelegate { 350 | 351 | func scrollViewDidScroll(scrollView: UIScrollView) { 352 | let component: UITableView = scrollView as! UITableView 353 | 354 | let center: CGPoint = component.superview!.convertPoint(component.center, toView: component) 355 | let indexPathAtCenter: NSIndexPath? = component.indexPathForRowAtPoint(center) 356 | 357 | if let visibleIndexPaths = component.indexPathsForVisibleRows() as? [NSIndexPath] { 358 | // Going through visible index paths and find the one which is at center 359 | // All cells should not be selected but that one. 360 | for indexPath:NSIndexPath in visibleIndexPaths { 361 | if let cell = component.cellForRowAtIndexPath(indexPath) { 362 | cell.setSelected(indexPath.isEqual(indexPathAtCenter), animated: true) 363 | } 364 | } 365 | } 366 | } 367 | 368 | func scrollViewWillEndDragging(scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer) { 369 | // When dragging end with no further animation and scrolling, velocity.y == 0, 370 | // component should scroll to the selected cell 371 | if velocity.y == 0 { 372 | scrollComponent(scrollView as! UITableView, toContentOffset: targetContentOffset.memory, animated: true) 373 | } 374 | } 375 | 376 | func scrollViewDidEndDecelerating(scrollView: UIScrollView) { 377 | // Scrolling to the selected cell 378 | scrollComponent(scrollView as! UITableView, toContentOffset: scrollView.contentOffset, animated: true) 379 | } 380 | } --------------------------------------------------------------------------------