OOMeetingRoomTableHeaderView.swift 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. //
  2. // OOMeetingRoomTableHeaderView.swift
  3. // o2app
  4. //
  5. // Created by 刘振兴 on 2018/1/18.
  6. // Copyright © 2018年 zone. All rights reserved.
  7. //
  8. import UIKit
  9. protocol OOMeetingRoomTableHeaderViewDelegate {
  10. //选择了指定的日期
  11. func setTheDate(_ startDate:String,_ endDate:String)
  12. }
  13. class OOMeetingRoomTableHeaderView: UIView {
  14. var delegate:OOMeetingRoomTableHeaderViewDelegate?
  15. @IBOutlet weak var selectedDateLabel: UILabel!
  16. @IBOutlet weak var theDateField: UITextField!
  17. @IBOutlet weak var theTimeField: UITextField!
  18. var startDate:String {
  19. let formatter = DateFormatter()
  20. formatter.dateFormat = "yyyy-MM-dd HH:mm"
  21. return formatter.string(from: currentDate)
  22. }
  23. var completedDate:String {
  24. // let endDate = Calendar.current.dateComponents([.hour,.minute], from: currentTime)
  25. // var dateComp = DateComponents()
  26. //
  27. // dateComp.hour = endDate.hour
  28. // dateComp.minute = endDate.minute
  29. // let eDate = Calendar.current.date(byAdding: dateComp, to: currentDate)
  30. var eDate = currentDate
  31. eDate = eDate.add(component: .hour, value: currentTime.hour)
  32. eDate = eDate.add(component: .minute, value: currentTime.minute)
  33. let formatter = DateFormatter()
  34. formatter.dateFormat = "yyyy-MM-dd HH:mm"
  35. return formatter.string(from: eDate)
  36. }
  37. var setDelegate:OOMeetingRoomTableHeaderViewDelegate?
  38. private var currentDate = Date()
  39. private var currentTime = Date()
  40. private let formatter = DateFormatter()
  41. private let dateFormat = "yyyy-MM-dd HH:mm"
  42. private let timeFormat = "HH时mm分"
  43. override func awakeFromNib() {
  44. formatter.dateFormat = dateFormat
  45. theDateField.text = formatter.string(from: currentDate)
  46. formatter.dateFormat = timeFormat
  47. currentTime = Calendar.current.date(bySettingHour: 1, minute: 0, second: 0, of: currentTime)!
  48. theTimeField.text = formatter.string(from: currentTime)
  49. theDateField.delegate = self
  50. theTimeField.delegate = self
  51. }
  52. //默认是当前时间 1个小时 这个函数是设置新的时间的
  53. func setChooseDate(startTime: Date, endTime:Date) {
  54. self.currentDate = startTime
  55. formatter.dateFormat = dateFormat
  56. theDateField.text = formatter.string(from: self.currentDate)
  57. self.currentTime = endTime
  58. var gap = endTime.hour - startTime.hour
  59. if gap < 0 {
  60. gap = 1
  61. }
  62. formatter.dateFormat = timeFormat
  63. currentTime = Calendar.current.date(bySettingHour: gap, minute: 0, second: 0, of: currentTime)!
  64. theTimeField.text = formatter.string(from: currentTime)
  65. }
  66. func callbackDelegate(){
  67. guard let block = setDelegate else {
  68. return
  69. }
  70. block.setTheDate(startDate, completedDate)
  71. }
  72. ///日期时间选择
  73. func datePicker(textField: UITextField) {
  74. let picker = QDatePicker{ (date: String) in
  75. print(date)
  76. textField.text = date
  77. self.callbackDelegate()
  78. }
  79. picker.themeColor = O2ThemeManager.color(for: "Base.base_color")!
  80. picker.datePickerStyle = .YMDHM
  81. picker.pickerStyle = .datePicker
  82. picker.showDatePicker(defaultDate: currentDate)
  83. }
  84. ///持续时间选择
  85. func timePicker(textField: UITextField) {
  86. let picker = QDatePicker{ (date: String) in
  87. let time = date.split(" ")[1]
  88. let dArray = time.split(":")
  89. textField.text = "\(dArray[0])时\(dArray[1])分"
  90. self.callbackDelegate()
  91. }
  92. picker.themeColor = O2ThemeManager.color(for: "Base.base_color")!
  93. picker.datePickerStyle = .HM
  94. picker.pickerStyle = .datePicker
  95. picker.showDatePicker(defaultDate: currentTime)
  96. }
  97. }
  98. extension OOMeetingRoomTableHeaderView:UITextFieldDelegate {
  99. func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
  100. if textField == theDateField {
  101. self.datePicker(textField: textField)
  102. return false
  103. }else if textField == theTimeField {
  104. self.timePicker(textField: textField)
  105. return false
  106. }
  107. return true
  108. }
  109. }