美文网首页
swift-日期选择器

swift-日期选择器

作者: 一蓑丨烟雨 | 来源:发表于2017-09-06 15:29 被阅读67次

    继上篇地址选择器,日期选择器同样继承自PickerBaseView。

    PickerBaseView详情参见地址(省-市-县)选择器
    效果:

    日期选择.png

    自定义DatePickerView

    import UIKit
    
    typealias DateResultBlock = (_ selectValue:String) -> Void
    
    class DatePickerView: PickerBaseView {
        
        var resultBlock:DateResultBlock?
        
        var datePickerMode:UIDatePickerMode = UIDatePickerMode.dateAndTime
        var title:String = ""
        var minDateStr:String = ""
        var maxDateStr:String = ""
        var selectValue:String = ""
        var isAutoSelect:Bool = true
        
        lazy var datePicker:UIDatePicker = {
            let picker = UIDatePicker(frame: CGRectMake(0, kTopViewHeight+0.5, SCREEN_WIDTH, kDatePicHeight))
            picker.backgroundColor = UIColor.white
            picker.datePickerMode = self.datePickerMode
            picker.locale = NSLocale.init(localeIdentifier: "zh_CHS_CN") as Locale
            
            picker.minimumDate = self.minDateStr.beDate
            picker.maximumDate = self.maxDateStr.beDate
            
            picker.setDate(Date(), animated: true)
            picker.addTarget(self, action: #selector(didSelectedValueChanged(sender:)), for: .valueChanged)
            
            return picker
        }()
        
        static func showDatePickerView(withTitle title:String, dateType type:UIDatePickerMode, defaultSelValue selValue:String, minDateStr min:String, maxDateStr max:String, isAutoSelect autoSelect:Bool, dateResultBlock resultBlock:@escaping DateResultBlock){
            let datePickerView = DatePickerView.pickerView(withTitle: title, dateType: type, defaultSelValue: selValue, minDateStr: min, maxDateStr: max, isAutoSelect: autoSelect, dateResultBlock: resultBlock)
            datePickerView.showWithAnimation(animate: true)
            
        }
        private override init(frame: CGRect) {
            super.init(frame: frame)
        }
        
        required init?(coder aDecoder: NSCoder) {
            fatalError("init(coder:) has not been implemented")
        }
        static func pickerView(withTitle title:String, dateType type:UIDatePickerMode, defaultSelValue selValue:String, minDateStr min:String, maxDateStr max:String, isAutoSelect autoSelect:Bool, dateResultBlock resultBlock:@escaping DateResultBlock) -> DatePickerView{
            let datePickerVeiw = DatePickerView()
            datePickerVeiw.datePickerMode = type
            datePickerVeiw.title = title
            datePickerVeiw.minDateStr = min
            datePickerVeiw.maxDateStr = max
            datePickerVeiw.isAutoSelect = autoSelect
            datePickerVeiw.resultBlock = resultBlock
            
            if selValue.utf8Length > 0 {
                datePickerVeiw.selectValue = selValue
            }else{
                datePickerVeiw.selectValue = datePickerVeiw.getDateByDatePickerMode()
            }
            datePickerVeiw.initUI()
            
            return datePickerVeiw
        }
        //值变化
        func didSelectedValueChanged(sender:UIDatePicker){
            self.selectValue = sender.date.ymdStr
            if isAutoSelect {
                self.resultBlock!(selectValue)
            }
        }
        //获取日期
        func getDateByDatePickerMode() -> String{
            var dateStr:String = ""
            switch self.datePickerMode {
            case UIDatePickerMode.time:
                dateStr = Date().hm
                break
            case UIDatePickerMode.date:
                dateStr = Date().ymd
                break
            case UIDatePickerMode.dateAndTime:
                dateStr = Date().ymdhm
                break
            case UIDatePickerMode.countDownTimer:
                dateStr = Date().hm
                break
    //        default:
    //            break
            }
            return dateStr
        }
        override func initUI() {
            super.initUI()
            self.titleLabel.text = title
            self.alertView.addSubview(self.datePicker)
        }
        
        func showWithAnimation(animate:Bool){
            UIApplication.shared.keyWindow?.addSubview(self)
            if animate {
                //初始位置
                var rect = self.alertView.frame
                rect.origin.y = SCREEN_HEIGHT
                self.alertView.frame = rect
                
                //浮现动画
                UIView.animate(withDuration: 0.3, animations: { 
                    var rect = self.alertView.frame
                    rect.origin.y -= kDatePicHeight+kTopViewHeight
                    self.alertView.frame = rect
                })
            }
        }
        //关闭动画
        func dismissWithAnimation(animate:Bool){
            UIView.animate(withDuration: 0.2, animations: { 
                var rect = self.alertView.frame
                rect.origin.y += kDatePicHeight+kTopViewHeight
                self.alertView.frame = rect
                self.backgroundView.alpha = 0
            }) { (finished) in
                self.leftBtn.removeFromSuperview()
                self.rightBtn.removeFromSuperview()
                self.titleLabel.removeFromSuperview()
                self.lineView.removeFromSuperview()
                self.topView.removeFromSuperview()
                self.alertView.removeFromSuperview()
                self.datePicker.removeFromSuperview()
                self.backgroundView.removeFromSuperview()
                self.removeFromSuperview()
                
            }
        }
    
    }
    extension DatePickerView{
        override func didTapBackgroundView() {
            self.dismissWithAnimation(animate: false)
        }
        override func clickLeftBtn() {
            self.dismissWithAnimation(animate: true)
        }
        override func clickRightBtn() {
            self.dismissWithAnimation(animate: true)
            self.resultBlock!(selectValue)
        }
    }
    
    

    使用方法和地址选择器一样:

        //日期选择
        lazy var dateTextField: TapTextField = {
            let dateT = TapTextField()
            dateT.placeholder = "请选择车险到期日期"
            dateT.delegate = self
            dateT.leftStr = "到期日期"
            return dateT
            
        }()
    
            dateTextField.setTapActionBlock { 
                DatePickerView.showDatePickerView(withTitle: "到期日期", dateType: UIDatePickerMode.date, defaultSelValue: "", minDateStr: Date().ymd, maxDateStr: "2020-12-31", isAutoSelect: true, dateResultBlock: { [unowned self](selectedValue) in
                    self.dateTextField.text = selectedValue
                })    
        
    

    相关文章

      网友评论

          本文标题:swift-日期选择器

          本文链接:https://www.haomeiwen.com/subject/mpcvjxtx.html