DatePickerPopupView.swift 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. //
  2. // DatePickerPopupView.swift
  3. // MTP2_iOS
  4. //
  5. // Created by zhongyuan on 2018/6/5.
  6. // Copyright © 2018年 zhongyuan.All rights reserved.
  7. //
  8. import UIKit
  9. protocol DatePickerPopupViewDelegate: class {
  10. /// 给页面上textfield赋值
  11. ///
  12. /// - Parameters:
  13. /// - sender: DatePickerPopupView
  14. /// - date: 当前选择的日期
  15. /// - value: 当前选择的值
  16. func datePickerPopupView(_ sender: DatePickerPopupView,selectedDate date: Date, selectedDateString value: String)
  17. }
  18. class DatePickerPopupView: UIView {
  19. /// 时间选择器
  20. @IBOutlet weak var datePicker: UIDatePicker!
  21. /// 遮罩视图
  22. @IBOutlet weak var maskPopupView: UIView!
  23. /// 弹出框有效组件的背景view
  24. @IBOutlet weak var backgroundView: UIView!
  25. @IBOutlet weak var heightConstraint: NSLayoutConstraint!
  26. //MARK: TAG值
  27. /// 取消按钮tag
  28. private static let CANCELBUTTONTAG = 100
  29. /// 确认按钮tag
  30. private static let CONFIRMBUTTONTAG = 101
  31. /// delegate
  32. weak var delegate: DatePickerPopupViewDelegate?
  33. /// 选择日期的String值
  34. private var value = ""
  35. @IBAction func buttonOnPressed(_ sender: UIBarButtonItem) {
  36. switch sender.tag {
  37. case DatePickerPopupView.CANCELBUTTONTAG:
  38. hidePopupView()
  39. case DatePickerPopupView.CONFIRMBUTTONTAG:
  40. if value == "" {
  41. valueChange(datePicker: datePicker)
  42. }
  43. delegate?.datePickerPopupView(self, selectedDate: datePicker.date, selectedDateString: value)
  44. default:
  45. break
  46. }
  47. }
  48. override func awakeFromNib() {
  49. super.awakeFromNib()
  50. bulidView()
  51. }
  52. /// 初始化设置
  53. private func bulidView() {
  54. /// 设置frame
  55. setNeedsLayout()
  56. layoutIfNeeded()
  57. /// 单击手势
  58. let ges = UITapGestureRecognizer(target: self, action: #selector(hidePopupView))
  59. self.maskPopupView.addGestureRecognizer(ges)
  60. /// datePicker
  61. datePicker.locale = Locale(identifier: "zh_cn")
  62. datePicker.timeZone = NSTimeZone.system
  63. datePicker.datePickerMode = .date
  64. datePicker.layer.backgroundColor = UIColor.white.cgColor
  65. datePicker.layer.masksToBounds = true
  66. datePicker.addTarget(self, action: #selector(valueChange(datePicker:)), for: .valueChanged)
  67. }
  68. /// 展示时间选择框
  69. ///
  70. /// - Parameters:
  71. /// - currentDate: 当前显示的时间
  72. /// - minimumDate: 可选择的最小时间
  73. func showInView(_ superView: UIView, currentDate: Date, minimumDate: Date? = nil, maximumDate: Date? = nil) {
  74. datePicker.date = currentDate
  75. datePicker.minimumDate = minimumDate
  76. datePicker.maximumDate = maximumDate
  77. superView.addSubview(self)
  78. self.heightConstraint.constant = 261.5
  79. UIView.animate(withDuration: 0.3, animations: {
  80. self.layoutIfNeeded()
  81. })
  82. self.frame = superView.frame
  83. }
  84. /// 移除当前 view
  85. @objc func hidePopupView() {
  86. self.heightConstraint.constant = 0
  87. UIView.animate(withDuration: 0.3, animations: {
  88. self.layoutIfNeeded()
  89. }) { (isSuccess) in
  90. self.removeFromSuperview()
  91. }
  92. }
  93. @objc func valueChange(datePicker: UIDatePicker) {
  94. let formatter = DateFormatter()
  95. formatter.dateFormat = "yyyy-MM-dd"
  96. let dateString = formatter.string(from: datePicker.date)
  97. value = dateString
  98. }
  99. }