美文网首页
DatePicker 实现

DatePicker 实现

作者: 光光6 | 来源:发表于2021-05-28 23:20 被阅读0次

class DatePickerViewController: UIViewController {

    varbackDate: ((Date) ->Void)?

    ///获取当前日期

    privatevarcurrentDateCom:DateComponents=Calendar.current.dateComponents([.year, .month, .day],  from:Date())    //日期类型

    varcontainV:UIView= {

//        let view = UIView(frame: CGRect(x: 0, y: ScreenInfo.Height-240, width: ScreenInfo.Width, height: 240))

        letview =UIView(frame:CGRect(x:0, y:240, width:ScreenInfo.Width, height:340))

        view.backgroundColor = UIColor.white

        returnview

    }()

    var backgroundView:UIView = {

        letview =UIView()

        view.backgroundColor=UIColor(red:0, green:0, blue:0, alpha:0.4)

        returnview

    }()

    var picker: UIPickerView!

    overrideinit(nibName nibNameOrNil:String?, bundle nibBundleOrNil:Bundle?) {

        super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)

    }

    requiredinit?(coder aDecoder:NSCoder) {

        fatalError("init(coder:) has not been implemented")

    }

    //MARK: - 生命周期

    override func viewDidLoad() {

        super.viewDidLoad()

        drawMyView()

    }

    //MARK: - Func

    private func drawMyView(){

        self.view.backgroundColor = UIColor.clear

        self.view.insertSubview(self.backgroundView, at:0)

        self.modalPresentationStyle = .custom//viewcontroller弹出后之前控制器页面不隐藏 .custom代表自定义

        letcancel =UIButton(frame:CGRect(x:0, y:10, width:70, height:20))

        letsure =UIButton(frame:CGRect(x:ScreenInfo.Width-80, y:10, width:70, height:20))

        cancel.setTitle("取消", for: .normal)

        sure.setTitle("确认", for: .normal)

        cancel.setTitleColor(UIColor.colorWithRGBA(r:255, g:51, b:102, a:1), for: .normal)

        sure.setTitleColor(UIColor.colorWithRGBA(r:255, g:51, b:102, a:1), for: .normal)

        cancel.addTarget(self, action:#selector(self.onClickCancel), for: .touchUpInside)

        sure.addTarget(self, action:#selector(self.onClickSure), for: .touchUpInside)

        picker=UIPickerView(frame:CGRect(x:0, y:24, width:ScreenInfo.Width, height:216))

        picker.delegate=self

        picker.dataSource=self

        picker.backgroundColor = UIColor.clear

        picker.clipsToBounds = true//如果子视图的范围超出了父视图的边界,那么超出的部分就会被裁剪掉。

        //创建日期选择器

        self.containV.addSubview(cancel)

        self.containV.addSubview(sure)

        self.containV.addSubview(picker)

        self.view.addSubview(self.containV)

        self.transitioningDelegate = self as UIViewControllerTransitioningDelegate//自定义转场动画

    }

    //MARK: onClick

    @objc func onClickCancel() {

        self.dismiss(animated:true, completion:nil)

    }

    @objc func onClickSure() {

        letdateString =String(format:"%02ld-%02ld-%02ld",self.picker.selectedRow(inComponent:0)+(self.currentDateCom.year!),self.picker.selectedRow(inComponent:1)+1,self.picker.selectedRow(inComponent:2)+1)

        letdateFormatter =DateFormatter()

        dateFormatter.dateFormat="YYYY-MM-dd"

        ///直接回调显示

        ifself.backDate!=nil{

            self.backDate!(dateFormatter.date(from: dateString) ??Date())

        }

        /*** 如果需求需要不能选择已经过去的日期

         let dateSelect = dateFormatter.date(from: dateString)

         let date = Date()

         let calendar = Calendar.current

         let dateNowString = String(format: "%02ld-%02ld-%02ld", calendar.component(.year, from: date) , calendar.component(.month, from: date), calendar.component(.day, from: date))

        /// 判断选择日期与当前日期关系

        let result:ComparisonResult = (dateSelect?.compare(dateFormatter.date(from: dateNowString)!))!

        if result == ComparisonResult.orderedAscending {

            /// 选择日期在当前日期之前,可以选择使用toast提示用户.

            return

            }else{

            /// 选择日期在当前日期之后. 正常调用

            if self.backDate != nil{

                self.backDate!(dateFormatter.date(from: dateString) ?? Date())

            }

        }

         */

        self.dismiss(animated:true, completion:nil)

    }

    ///点击任意位置view消失

    overridefunctouchesBegan(_touches:Set, with event:UIEvent?) {

        super.touchesBegan(touches, with: event)

        self.dismiss(animated:true, completion:nil)

    }

    override func didReceiveMemoryWarning() {

        super.didReceiveMemoryWarning()

        // Dispose of any resources that can be recreated.

    }

}

//MARK: - PickerViewDelegate

extension EWDatePickerViewController:UIPickerViewDelegate,UIPickerViewDataSource {

    funcnumberOfComponents(in pickerView:UIPickerView) ->Int{

        return3

    }

    funcpickerView(_pickerView:UIPickerView, numberOfRowsInComponent component:Int) ->Int{

        ifcomponent==0{

            return10

        }elseifcomponent==1{

            return12

        }else{

            letyear:Int= pickerView.selectedRow(inComponent:0)+currentDateCom.year!

            letmonth:Int= pickerView.selectedRow(inComponent:1)+1

            letdays:Int=howManyDays(inThisYear: year, withMonth: month)

            returndays

        }

    }

    privatefunchowManyDays(inThisYear year:Int, withMonth month:Int) ->Int{

        if(month==1)||(month==3)||(month==5)||(month==7)||(month==8)||(month==10)||(month==12) {

            return31

        }

        if(month==4)||(month==6)||(month==9)||(month==11) {

            return30

        }

        if(year%4==1)||(year%4==2)||(year%4==3) {

            return28

        }

        ifyear%400==0{

            return29

        }

        ifyear%100==0{

            return28

        }

        return29

    }

    funcpickerView(_pickerView:UIPickerView, widthForComponent component:Int) ->CGFloat{

        returnScreenInfo.Width/3

    }

    funcpickerView(_pickerView:UIPickerView, rowHeightForComponent component:Int) ->CGFloat{

        return40

    }

    funcpickerView(_pickerView:UIPickerView, titleForRow row:Int, forComponent component:Int) ->String? {

        ifcomponent==0{

            return"\((currentDateCom.year!)+row)\("年")"

        }elseifcomponent==1{

            return"\(row+1)\("月")"

        }else{

            return"\(row+1)\("日")"

        }

    }

    funcpickerView(_pickerView:UIPickerView, didSelectRow row:Int, inComponent component:Int) {

        ifcomponent==1{

            pickerView.reloadComponent(2)

        }

    }

}

//MARK: - 转场动画delegate

extension EWDatePickerViewController:UIViewControllerTransitioningDelegate{

    func animationController(forPresented presented: UIViewController, presenting: UIViewController, source: UIViewController) -> UIViewControllerAnimatedTransitioning? {

        letanimated =EWDatePickerPresentAnimated(type: .present)

        returnanimated

    }

    func animationController(forDismissed dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {

        letanimated =EWDatePickerPresentAnimated(type: .dismiss)

        returnanimated

    }

}

相关文章

网友评论

      本文标题:DatePicker 实现

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