123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143 |
- //
- // OOMeetingRoomTableHeaderView.swift
- // o2app
- //
- // Created by 刘振兴 on 2018/1/18.
- // Copyright © 2018年 zone. All rights reserved.
- //
- import UIKit
- protocol OOMeetingRoomTableHeaderViewDelegate {
- //选择了指定的日期
- func setTheDate(_ startDate:String,_ endDate:String)
- }
- class OOMeetingRoomTableHeaderView: UIView {
-
- var delegate:OOMeetingRoomTableHeaderViewDelegate?
-
- @IBOutlet weak var selectedDateLabel: UILabel!
-
- @IBOutlet weak var theDateField: UITextField!
-
- @IBOutlet weak var theTimeField: UITextField!
-
-
-
- var startDate:String {
- let formatter = DateFormatter()
- formatter.dateFormat = "yyyy-MM-dd HH:mm"
-
- return formatter.string(from: currentDate)
- }
-
- var completedDate:String {
- // let endDate = Calendar.current.dateComponents([.hour,.minute], from: currentTime)
- // var dateComp = DateComponents()
- //
- // dateComp.hour = endDate.hour
- // dateComp.minute = endDate.minute
-
- // let eDate = Calendar.current.date(byAdding: dateComp, to: currentDate)
- var eDate = currentDate
-
- eDate = eDate.add(component: .hour, value: currentTime.hour)
- eDate = eDate.add(component: .minute, value: currentTime.minute)
- let formatter = DateFormatter()
- formatter.dateFormat = "yyyy-MM-dd HH:mm"
-
- return formatter.string(from: eDate)
-
- }
-
- var setDelegate:OOMeetingRoomTableHeaderViewDelegate?
-
- private var currentDate = Date()
-
- private var currentTime = Date()
-
- private let formatter = DateFormatter()
-
- private let dateFormat = "yyyy-MM-dd HH:mm"
-
- private let timeFormat = "HH时mm分"
-
-
-
- override func awakeFromNib() {
- formatter.dateFormat = dateFormat
- theDateField.text = formatter.string(from: currentDate)
- formatter.dateFormat = timeFormat
- currentTime = Calendar.current.date(bySettingHour: 1, minute: 0, second: 0, of: currentTime)!
- theTimeField.text = formatter.string(from: currentTime)
- theDateField.delegate = self
- theTimeField.delegate = self
- }
-
- //默认是当前时间 1个小时 这个函数是设置新的时间的
- func setChooseDate(startTime: Date, endTime:Date) {
- self.currentDate = startTime
- formatter.dateFormat = dateFormat
- theDateField.text = formatter.string(from: self.currentDate)
- self.currentTime = endTime
- var gap = endTime.hour - startTime.hour
- if gap < 0 {
- gap = 1
- }
- formatter.dateFormat = timeFormat
- currentTime = Calendar.current.date(bySettingHour: gap, minute: 0, second: 0, of: currentTime)!
- theTimeField.text = formatter.string(from: currentTime)
- }
-
- func callbackDelegate(){
- guard let block = setDelegate else {
- return
- }
- block.setTheDate(startDate, completedDate)
- }
-
-
- ///日期时间选择
- func datePicker(textField: UITextField) {
- let picker = QDatePicker{ (date: String) in
- print(date)
- textField.text = date
- self.callbackDelegate()
- }
- picker.themeColor = O2ThemeManager.color(for: "Base.base_color")!
- picker.datePickerStyle = .YMDHM
- picker.pickerStyle = .datePicker
- picker.showDatePicker(defaultDate: currentDate)
- }
-
- ///持续时间选择
- func timePicker(textField: UITextField) {
- let picker = QDatePicker{ (date: String) in
- let time = date.split(" ")[1]
- let dArray = time.split(":")
- textField.text = "\(dArray[0])时\(dArray[1])分"
- self.callbackDelegate()
- }
- picker.themeColor = O2ThemeManager.color(for: "Base.base_color")!
- picker.datePickerStyle = .HM
- picker.pickerStyle = .datePicker
- picker.showDatePicker(defaultDate: currentTime)
- }
- }
- extension OOMeetingRoomTableHeaderView:UITextFieldDelegate {
- func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
-
- if textField == theDateField {
- self.datePicker(textField: textField)
- return false
- }else if textField == theTimeField {
- self.timePicker(textField: textField)
- return false
- }
- return true
- }
- }
|