ELPickerView 4.0.1

ELPickerView 4.0.1

测试已测试
语言语言 SwiftSwift
许可 MIT
发布最后发布2017年11月
SwiftSwift 版本4.0
SPM支持 SPM

Xu Hanping 维护。




ELPickerView:使用 Swift 4 构建的易于使用的 Picker View

Build Status
CocoaPods
CocoaPods
Swift 4.0
iOS 8.0

⚠️ELPickerView 需要 Swift 版本高于 4.0.

请点击中文说明

截图

使用 Swift 4 构建的易于使用的 Picker View

screenshots_4

如何安装

使用 CocoaPods

  • 将以下行添加到您的 podfile
    pod 'ELPickerView'
  • 使用终端运行 pod install
    Swift 3. 使用 3.0.0 pod 版本
    Swift 4. 使用 4.* pod 版本
  • 然后一切就绪!

只需添加

  • 使用浏览器打开 Elenionl/ELPickerView-Swift
  • 下载或克隆项目: https://github.com/Elenionl/ELPickerView-Swift.git
  • ELCustomPickerView.swift 文件复制到您的项目中
  • 享受吧

如何使用

如果您想在您的应用程序中显示一个 Picker View,只需执行以下两个步骤

  • 初始化
lazy var customPickerView: ELCustomPickerView<String> = {
    return ELCustomPickerView<String>(pickerType: .singleComponent, items: [
        "Row 0"
        , "Row 1"
        , "Row 2"
        , "Row 3"
        , "Row 4"
        , "Row 5"
        ])
}
  • 显示
override func viewDidLoad() {
        super.viewDidLoad()
        customPickerView.show(viewController: self, animated: true)
}

如果您想使半透明的背景覆盖导航栏和标签背景

  • 只需让窗口以这种方式显示 Picker View
    override func viewDidLoad() {
        super.viewDidLoad()
        customPickerView.show(viewController: nil, animated: true)
    }

与委托不同,ELPickerView 使用闭包来处理回调

  • 下面有丰富的回调
    使用一系列'Set'方法
        view.setDidScrollHandler({ [weak self] (view, chosenIndex, chosenItem) -> (shouldHide: Bool, animated: Bool) in
            let hide = false
            let animated = false
            self?.logLabel.text = "Did Scroll. \n<Index: \(chosenIndex)> \n<chosenItem: \(chosenItem)> \n<Hide: \(hide)> \n<Animated: \(animated)>"
            print(self?.logLabel.text ?? "")
            return (hide, animated)
        })

设置值

        customPickerView.leftButtoTapHandler = { [weak self] (view: ELCustomPickerView<String>?, chosenIndex: Int, chosenItem: (key: String, content: String)) in
            let hide = true
            let animated = true
            self?.logLabel.text = "Did Tap Left Button. <Index: \(chosenIndex)> <chosenItem: \(chosenItem)> <Hide: \(hide)> <Animated: \(animated)>"
            print(self?.logLabel.text ?? "")
            return (hide, animated)
        }
  • 以下是处理器定义及其含义
    /// Triggered when Left Button is tapped.
    //  view: the CustomPickerView
    //  chosenIndex: the current chosen index of row in Picker View
    //  chosenItem: the Item connected with the chosen row
    //  shouldHide: tell the Picker View whether it should be hide  Default value is true
    //  animated: tell the Picker View whether the hide action should have animation  Default value is true
    public var leftButtoTapHandler: ((_ view: ELCustomPickerView?, _ chosenIndex: Int, _ chosenItem: T) -> (shouldHide: Bool, animated: Bool))?

您可以让用户在实例、结构体、枚举、元组之间进行选择

  • 在初始化时添加任何类型的项
typealias CustomView = ELCustomPickerView<(key: String, content: String)>
...
let view = CustomView(pickerType: .singleComponent, items: [
          ("00", "Row 0")
          , ("02", "Row 1")
          , ("04", "Row 2")
          , ("06", "Row 3")
          , ("09", "Row 4")
          , ("11", "Row 5")
          ])
  • 提供一个处理程序以将项转换为类型为 var 的字符串
view.itemConfigHandler = { (key: String, content: String) in
    return content
}
  • 然后完成

视图易于自定义

        view.blackBackground = true
        view.isTitleBarHidden = false
        view.isTapBackgroundEnabled = true
        view.leftButton.setTitle("LEFT", for: .normal)
        view.rightButton.setTitle("RIGHT", for: .normal)
        view.title.text = "TITLE"
        view.foregroundView.picker.backgroundColor = UIColor.white
        view.foregroundView.bottomDivider.isHidden = true

有设置和处理器可用

// MARK: - Settings

/// Type of Picker View
public let pickerType: ELCustomPickerViewType

/// Items used to config Picker View rows  Default value is []
public var items: [T]

/// Background of the screen  Default value is true
public var blackBackground: Bool

/// Set Title Bar hidden or not  Default value is false
public var isTitleBarHidden = false

/// Set Taping Background to hide Picker View enabled or not  Default value is true
public var isTapBackgroundEnabled = true

/// Left Button of the Title Bar, shortcut to foregroundView.leftButton
public lazy var leftButton: UIButton

/// Right Button of the Title Bar, shortcut to foregroundView.rightButton
public lazy var rightButton: UIButton

/// Title of the Title Bar, shortcut to foregroundView.title
public lazy var title: UILabel


// MARK: - Handler

/// Function used to transform Item into String. If the Item is String kind, itemConfigHandler is not necessory to be set.
public var itemConfigHandler: ((T) -> String)?

/// Triggered when Left Button is tapped.
//  view: the CustomPickerView
//  chosenIndex: the current chosen index of row in Picker View
//  chosenItem: the Item connected with the chosen row
//  shouldHide: tell the Picker View whether it should be hide  Default value is true
//  animated: tell the Picker View whether the hide action should have animation  Default value is true
public var leftButtoTapHandler: ((_ view: ELCustomPickerView?, _ chosenIndex: Int, _ chosenItem: T) -> (shouldHide: Bool, animated: Bool))?

/// Triggered when Right Button is tapped.
//  view: the CustomPickerView
//  chosenIndex: the current chosen index of row in Picker View
//  chosenItem: the Item connected with the chosen row
//  shouldHide: tell the Picker View whether it should be hide  Default value is true
//  animated: tell the Picker View whether the hide action should have animation  Default value is true
public var rightButtoTapHandler: ((_ view: ELCustomPickerView?, _ chosenIndex: Int, _ chosenItem: T) -> (shouldHide: Bool, animated: Bool))?

/// Triggered when user picked one row in Picker View.
//  view: the CustomPickerView
//  chosenIndex: the current chosen index of row in Picker View
//  chosenItem: the Item connected with the chosen row
//  shouldHide: tell the Picker View whether it should be hide  Default value is false
//  animated: tell the Picker View whether the hide action should have animation   Default value is false
public var didScrollHandler: ((_ view: ELCustomPickerView?, _ chosenIndex: Int, _ chosenItem: T) -> (shouldHide: Bool, animated: Bool))?

/// Triggered when Picker View will show
public var willShowHandler: ((_ view: ELCustomPickerView?) -> Void)?

/// Triggered when Picker View did show
public var didShowHandler: ((_ view: ELCustomPickerView?) -> Void)?

/// Triggered when Picker View will hide
public var willHideHandler: ((_ view: ELCustomPickerView?) -> Void)?

/// Triggered when Picker View did hide
public var didHideHandler: ((_ view: ELCustomPickerView?) -> Void)?

// MARK: - Views

/// The bottom view containing Title Bar and Picker
public lazy var foregroundView: ELPickerForegroundView

要求

  • Xcode 8.0
  • Swift 4.0
  • 使用 ARC
  • iOS 8.0

待办事项

  • 具有多个组件的 Picker View
  • 日期和时间 Picker View

作者

Xu Hanping (Elenionl), [email protected]


许可

ELPickerView 在 MIT 许可下可用,有关更多信息,请参阅 LICENSE 文件。