Swift 自定义PickerView,支持选择时间、单选、多选

bg:

系统提供了时间选择器UIDatePicker、文本选择器UIPickerView,这两种选择器本身具备了很多特性及优点。但在实际项目开发中有较多地方用到时间选择或数据选择场景,比如商城类App购物车的商品添加、有预约场景的App时间及事务选择等,考虑到UI要求、代码封装易用性等方面,就需要我们自己自定义一些选择控件了。

这里自定义一个简单的选择器:时间选择单选多选(暂未添加多级联动选择模式,后续添加)其中时间选择支持设置时间范围和默认时间

Demo地址:https://github.com/wanghhh/HHPickerViewDemo

先看下效果图:

pickerView_gif.gif
调用示例:

比如选择时间:

let pickerView = HHPickerView.init(frame: CGRect.init(x: marginTop, y: self.view.bounds.size.height, width: self.view.bounds.size.width, height: CGFloat(toolBarH + pickerViewH)), dateFormat: nil,datePickerMode:.dateAndTime, minAndMaxAndCurrentDateArr: nil)
        pickerView.rowAndComponentCallBack = {(resultStr,selectedArr) in
            print("str--->\(String(describing: resultStr))")
            btn.setTitle(resultStr! as String, for: .normal)
        }
pickerView.show()

//多选

let pickerView = HHPickerView.init(frame: CGRect.init(x: marginTop, y: self.view.bounds.size.height, width: self.view.bounds.size.width, height: CGFloat(toolBarH + pickerViewH)), dataSource: data as NSArray, defaultIntegerArr: [1,3,6], pickerType: .mutable)
        
        pickerView.rowAndComponentCallBack = {(resultStr,selectedArr) in
            print("str--->\(String(describing: resultStr))")
            btn.setTitle(resultStr! as String, for: .normal)
        }
 pickerView.show()

代码结构:

ic_picker01.png

调用时将HHPickerView.swift文件拖进项目即可,顾名思义HHPickerView类就是自定义的选择器。
由于要支持时间选择、文本选择,就抽取了对应的2个辅助类(暂且这样叫):HHDatePicker(时间选择类)、HHCollectionView(文本选择类)。其中HHDatePicker直接继承自系统的UIDatePicker,HHCollectionView这里就继承自UICollectionView实现了,也可以直接调用辅助类中的方法。

HHPickerView实现:

//选择器类型

enum HHPickerViewType:NSInteger {
   case single = 0   //只能单选
   case mutable = 1  //可多选、单选
   case time = 2     //选择时间
}
定义一个结果回调闭包

//返回选择结果内容、结果索引数组(针对单选、多选)

typealias HHPickerViewCallBackClosure = (_ resultStr:NSString?,_ resultArr:NSArray?) -> ()

var rowAndComponentCallBack:HHPickerViewCallBackClosure?//选择内容回调

重写init方法来实现基本UI设置,代码如下:

override init(frame: CGRect) {
    super.init(frame: frame)
    self.backgroundColor = UIColor.white;
    
    // 1 获取window
    if (keyWindow == nil) {
        self.keyWindow = UIApplication.shared.keyWindow
    }
    // 2.遮罩view
    overlayView = UIControl.init(frame: UIScreen.main.bounds)
    overlayView?.backgroundColor = UIColor.init(red: 0, green: 0, blue: 0, alpha: 0.5)
    overlayView?.addTarget(self, action: #selector(hide), for: .touchUpInside)
    overlayView?.alpha = 0
    // 3.创建工具条toolView
    let toolView:UIView = UIView.init(frame: CGRect.init(x: 0, y: 0, width: Int(self.bounds.size.width), height: Int(toolBarH)))
    toolView.backgroundColor = UIColor(red: 230/255, green: 230/255, blue: 230/255, alpha: 1)
    addSubview(toolView)
    
    cancelButton = UIButton.init(frame: CGRect.init(x: btnMargin, y: 0, width: 44, height: toolView.bounds.size.height))
    cancelButton?.setTitle("取消", for: .normal)
    cancelButton?.setTitleColor(cancelTextNormalColor, for: .normal)
    cancelButton?.setTitleColor(cancelTextSelectedColor, for: .selected)
    cancelButton?.titleLabel?.font = UIFont.systemFont(ofSize: 17.5)
    cancelButton?.contentHorizontalAlignment = .left
    cancelButton?.addTarget(self, action: #selector(cancelAction), for: .touchUpInside)
    toolView.addSubview(cancelButton!)
    
    confirmButton = UIButton.init(frame: CGRect.init(x: (toolView.bounds.size.width - 44.0 - btnMargin), y: 0, width: 44, height: toolView.bounds.size.height))
    confirmButton?.setTitle("确定", for: .normal)
    confirmButton?.setTitleColor(confirmTextNormalColor, for: .normal)
    confirmButton?.setTitleColor(confirmTextSelectedColor, for: .selected)
    confirmButton?.titleLabel?.font = UIFont.systemFont(ofSize: 17.5)
    confirmButton?.contentHorizontalAlignment = .left
    confirmButton?.addTarget(self, action: #selector(confirmAction), for: .touchUpInside)
    toolView.addSubview(confirmButton!)
}
时间选择模式便利方法:(可设置dateFormat格式化字符串;选择器datePickerMode;时间范围控制:可选最小、最大时间及默认时间)
/// 时间选择便利构造器
///
/// - Parameters:
///   - frame: frame
///   - dateFormat: 时间格式化字符串,可空
///   - datePickerMode: 选择器的时间模式,可空
///   - minAndMaxAndCurrentDateArr: 可选最小、最大时间及当前时间,可空
convenience init(frame: CGRect,dateFormat:NSString?,datePickerMode:UIDatePickerMode?,minAndMaxAndCurrentDateArr:[NSDate]?) {
    self.init(frame: frame)
    pickerViewType = HHPickerViewType.time
    
    let picker = HHDatePicker.init(frame: CGRect.init(x: (confirmButton?.superview?.frame.minX)!, y: (confirmButton?.superview?.frame.maxY)!, width: UIScreen.main.bounds.size.width, height: CGFloat(pickerViewH)), dateFormat: dateFormat,datePickerMode:datePickerMode, minAndMaxAndCurrentDateArr: nil, resultCallBack: {[weak self] (resultStr) in
        self?.blockContent = resultStr
    })
    picker.getSelectedResult({[weak self] (resultStr) in
        self?.blockContent = resultStr
    })
    addSubview(picker)
}
单选、多选模式便利构造方法:(可设置单选、多选模式;可设置默认选中的选项)
/// 单选/多选便利构造器
///
/// - Parameters:
///   - frame: frame
///   - pickerType: 选择类型(单选或多选)
///   - dataSource: 数据源
///   - defaultIntegerArr:  默认选中项的索引数组
convenience init(frame: CGRect,dataSource:NSArray,defaultIntegerArr:NSArray?,pickerType:HHPickerViewType) {
    self.init(frame: frame)
    pickerViewType = pickerType
    if (dataSource.count != 0) {
        let picker = HHCollectionView.init(frame: CGRect.init(x: (confirmButton?.superview?.frame.minX)!, y: (confirmButton?.superview?.frame.maxY)!, width: UIScreen.main.bounds.size.width, height: CGFloat(pickerViewH)), collectionViewLayout: HHWaterfallLayout(), dataSource: dataSource, defaultIntegerArr: defaultIntegerArr, contentCallBack: { [weak self] (resultStr, selectedArr) in
            self?.blockContent = resultStr
            self?.selectedArr = selectedArr
        })
        picker.rowAndComponentCallBack = {[weak self](resultStr,selectedArr) in
            self?.blockContent = resultStr
            self?.selectedArr = selectedArr
        }
        addSubview(picker)
    }else{
        assert(dataSource.count != 0, "dataSource is not allowed to be nil")
    }
}
主要事件处理

//显示

func show(){
    keyWindow?.addSubview(overlayView!)
    keyWindow?.addSubview(self)
    UIView.animate(withDuration: 0.25, animations: {
        self.overlayView?.alpha = 1.0
        var frame = self.frame
        frame.origin.y = UIScreen.main.bounds.size.height - self.bounds.size.height
        self.frame = frame
    }) { (isFinished) in
        //
    }
}

//隐藏

func hide() {
    self.dismissCallBack()
    UIView.animate(withDuration: 0.25, animations: {
        self.overlayView?.alpha = 0
        var frame = self.frame
        frame.origin.y = UIScreen.main.bounds.size.height
        self.frame = frame
    }) { (isFinished) in
        self.overlayView?.removeFromSuperview()
        self.removeFromSuperview()
    }
}

//取消选择

func cancelAction() {
    hide()
}

//确定选择

func confirmAction() {
    if blockContent == "" {
        showAlert(withTitle: "提示", message: "未选择任何一项!")
    }else if pickerViewType != HHPickerViewType.time && (selectedArr?.count)! > 1 && pickerViewType == HHPickerViewType.single {
        showAlert(withTitle: "提示", message: "此项仅支持单选!")
    }else{
        self.rowAndComponentCallBack!(blockContent,selectedArr)
    }
    hide()
}

//异常提示

@objc private func showAlert(withTitle title: String?, message: String?) {
    let alertVc = UIAlertController.init(title: title, message: message, preferredStyle: UIAlertControllerStyle.alert)
    alertVc.addAction(UIAlertAction.init(title: "我知道了", style: UIAlertActionStyle.cancel, handler: nil))
    UIApplication.shared.keyWindow?.rootViewController?.present(alertVc, animated: true, completion: nil)
}

以上就是HHPickerView的主要实现,代码简单,就不再详细说明了,可查看Demo:https://github.com/wanghhh/HHPickerViewDemo

HHDatePicker实现:

//结果回调闭包

typealias HHDatePickerCallBackClosure = (_ resultStr:NSString?) -> ()

var dateChangeCallBack:HHDatePickerCallBackClosure? //时间改变回调
便利构造方法:
/// 时间选择器便利构造方法
///
/// - Parameters:
///   - frame: frame
///   - dateFormat: 时间格式化字符串
///   - datePickerMode: 选择器的时间模式
///   - minAndMaxAndCurrentDateArr: 可选最小、最大时间及当前时间
///   - resultCallBack: 选择结果
convenience init(frame: CGRect,dateFormat:NSString?,datePickerMode:UIDatePickerMode?,minAndMaxAndCurrentDateArr:[NSDate]?,resultCallBack:((_ resultStr:NSString) -> Void)?) {
    self.init(frame: frame)
    self.backgroundColor = UIColor.white;
    if datePickerMode != nil {
        self.datePickerMode = datePickerMode!
    }else{
        self.datePickerMode = .dateAndTime //默认显示月、日、时间
    }
    if dateFormat?.range(of: "yy").location != NSNotFound {
        self.datePickerMode = .dateAndTime
    }else{
        self.datePickerMode = .date
    }
    //可以设置时间范围
    var minDateTem = NSDate.init()
    var maxDateTem = NSDate.init(timeIntervalSinceNow: 90*365*24*60*60)
    var currentDateTem = NSDate.init()
    if minAndMaxAndCurrentDateArr != nil && minAndMaxAndCurrentDateArr?.count == 2 {
        minDateTem = (minAndMaxAndCurrentDateArr?[0])!
        maxDateTem = (minAndMaxAndCurrentDateArr?[1])!
        currentDateTem = (minAndMaxAndCurrentDateArr?[2])!
    }
    self.minimumDate = minDateTem as Date
    self.maximumDate = maxDateTem as Date
    self.setDate(currentDateTem as Date, animated: false)
    self.locale = Locale.init(identifier: "zh_CN")
    
    self.addTarget(self, action: #selector(dateChange(datePicker:)), for: UIControlEvents.valueChanged)
    
    //默认回调当前时间
    let theDate = self.date
    let dateFormatter = DateFormatter.init()
    if (dateFormat != nil) {
        dateFormatter.dateFormat = dateFormat! as String
        self.dateFormat = dateFormat
    }else{
        dateFormatter.dateFormat = "YYYY-MM-dd HH:mm:ss"
        self.dateFormat = dateFormatter.dateFormat! as NSString
    }
    let nowDate = dateFormatter.string(from: theDate)
    resultCallBack!(nowDate as NSString)
}

//时间改变结果回调

fileprivate func getSelectedResult(_ callBack: @escaping(HHDatePickerCallBackClosure)) {
    dateChangeCallBack = callBack
}

//时间改变监听

func dateChange(datePicker:UIDatePicker) {
    let theDate = datePicker.date
    print("\(theDate.description(with: Locale.current))")
    
    let dateFormatter = DateFormatter.init()
    dateFormatter.dateFormat = self.dateFormat! as String
    let nowDate = dateFormatter.string(from: theDate)

    dateChangeCallBack!(nowDate as NSString)
}
HHCollectionView主要实现:

//回调

fileprivate var rowAndComponentCallBack:HHPickerViewCallBackClosure?//选择内容回调
lazy var dataSourceArr = NSMutableArray() //数据源
lazy var selectedArr = NSMutableArray() //被选中的数据
便利方法:
/// 便利构造器
///
/// - Parameters:
///   - frame: frame
///   - collectionViewLayout: collectionViewLayout
///   - dataSource: 选择项数据源
///   - defaultIntegerArr: 默认选中的项索引数组
///   - contentCallBack: 选择结果回调
convenience init(frame:CGRect,collectionViewLayout:UICollectionViewLayout,dataSource:NSArray,defaultIntegerArr:NSArray?,contentCallBack:HHPickerViewCallBackClosure?) {
    self.init(frame: frame, collectionViewLayout: collectionViewLayout)
    self.delegate = self
    self.dataSource = self
    self.backgroundColor = UIColor.white
    self.dataSourceArr = NSMutableArray.init(array: dataSource)
    if (defaultIntegerArr != nil) {
        self.selectedArr = NSMutableArray.init(array: defaultIntegerArr!)
    }
    
    self.register(HHCollectionCell.self, forCellWithReuseIdentifier: HHCollectionViewCellId)
    
    if (contentCallBack != nil) {
        //默认选中数据
        var resultStr = "" //选中的结果的拼接字符串,多选用“;”号隔开(按需要自定义)
        
        if self.selectedArr.count > 0 {
            for (idx,obj) in self.selectedArr.enumerated() {
                if idx == 0 {
                    resultStr = self.dataSourceArr[(obj as? Int)!] as! String
                }else{
                    resultStr = "\(resultStr);\(self.dataSourceArr[(obj as? Int)!])"
                }
            }
        }
        contentCallBack!(resultStr as NSString,selectedArr)
    }
}
在UICollectionViewDelegate中回调结果:
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    let cell:HHCollectionCell = collectionView.cellForItem(at: indexPath) as! HHCollectionCell        
    if (self.selectedArr.count>0) {
        var isExited = false//是否已经被选中,即存在selectedArr中
        for (_,obj) in self.selectedArr.enumerated() {
            if obj as? NSInteger == indexPath.row{
                cell.isSelected = false //取消选中
                isExited = true
                selectedArr.remove(indexPath.row)
                break
            }
        }
        if isExited == false {
            selectedArr.add(indexPath.row)
        }
    }else{
        cell.isSelected = true
        selectedArr.add(indexPath.row)
    }
    reloadItems(at: [indexPath])
    
    //组装回调结果***
    //默认选中数据
    var resultStr = "" //选中的结果的拼接字符串,多选用“;”号隔开(按需要自定义)
    
    if self.selectedArr.count > 0 {
        for (idx,obj) in self.selectedArr.enumerated() {
            if idx == 0 {
                resultStr = self.dataSourceArr[(obj as? Int)!] as! String
            }else{
                resultStr = "\(resultStr);\(self.dataSourceArr[(obj as? Int)!])"
            }
        }
    }
    self.rowAndComponentCallBack!(resultStr as NSString,selectedArr)
}
HHCollectionView的布局及自定义cell的代码比较简单就不在bia了,详细代码可以下载demo:https://github.com/wanghhh/HHPickerViewDemo查看。
本文所涉及到代码都比较简单,主要提供一个思路:通过自定义view+自定义datePicker+自定义文本选择view实现时间选择、单选及多选。大家可以根据项目需要比如UI需求等,予以扩展、完善。

欢迎大家指出错误、相互交流,共同学习!

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 158,233评论 4 360
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,013评论 1 291
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 108,030评论 0 241
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,827评论 0 204
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,221评论 3 286
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,542评论 1 216
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,814评论 2 312
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,513评论 0 198
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,225评论 1 241
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,497评论 2 244
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 31,998评论 1 258
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,342评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 32,986评论 3 235
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,055评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,812评论 0 194
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,560评论 2 271
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,461评论 2 266

推荐阅读更多精彩内容