QCalendarPicker.swift 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592
  1. //
  2. // QCalendarPicker.swift
  3. // O2Platform
  4. // 来自GitHub上的https://github.com/qyfeng009/QTimePicker
  5. //
  6. // Created by FancyLou on 2019/5/8.
  7. // Copyright © 2019 zoneland. All rights reserved.
  8. //
  9. import UIKit
  10. private let screenWidth = UIScreen.main.bounds.width
  11. private let screenHeight = UIScreen.main.bounds.height
  12. private let keyWindow = UIApplication.shared.keyWindow
  13. private let calendarItemWH: CGFloat = (screenWidth - 20 - 6 * 8) / 7
  14. private var baseViewHeight = 49 + 30 + 6 * calendarItemWH + 6 * 8
  15. ///日历选择器 可以选择日期 、日期+时间、日期间隔
  16. class QCalendarPicker: UIView, UIGestureRecognizerDelegate, CalendarViewDelegate, TimePickerViewDelegate {
  17. typealias DidSelectedDate = (_ date: String) -> Void
  18. enum QCalendarPickerStyle {
  19. case datePicker //日期选择器
  20. case dateTimePicker // 日期时间选择器
  21. case dateIntervalPicker // 日期区间选择器
  22. }
  23. private var baseView: UIView!
  24. private var dateBtn: UIButton!
  25. private var endDateBtn: UIButton!
  26. private var timeBtn: UIButton!
  27. private var currentDate: String!
  28. private var endDate: String!
  29. private var currentTime: String!
  30. private var cursorView: UIView!
  31. private var selectedBack: DidSelectedDate?
  32. private lazy var calendarView: CalendarView! = {
  33. var calendarViewH = baseView.height - dateBtn.height
  34. if screenHeight == 812 {
  35. calendarViewH = baseView.height - dateBtn.height - 34
  36. }
  37. let calendarView = CalendarView(frame: CGRect(x: 0, y: dateBtn.height, width: screenWidth, height: calendarViewH))
  38. calendarView.delegate = self
  39. return calendarView
  40. }()
  41. private lazy var timePickerView: TimePickerView! = {
  42. let timePickerView = TimePickerView(frame: calendarView.frame)
  43. timePickerView.x = baseView.width
  44. timePickerView.delegate = self
  45. return timePickerView
  46. }()
  47. // open var isAllowSelectTime: Bool? = true {
  48. // didSet {
  49. // if isAllowSelectTime == true {
  50. // timeBtn.isHidden = false
  51. // } else {
  52. // timeBtn.isHidden = true
  53. // }
  54. // }
  55. // }
  56. open var calendarPickerStyle: QCalendarPickerStyle = .datePicker {
  57. didSet {
  58. switch calendarPickerStyle {
  59. case .datePicker:
  60. timeBtn.isHidden = true
  61. endDateBtn.isHidden = true
  62. case .dateTimePicker:
  63. timeBtn.isHidden = false
  64. endDateBtn.isHidden = true
  65. case .dateIntervalPicker:
  66. timeBtn.isHidden = true
  67. endDateBtn.isHidden = false
  68. }
  69. }
  70. }
  71. init(selectedDate: @escaping DidSelectedDate) {
  72. super.init(frame: UIScreen.main.bounds)
  73. let tapGesture = UITapGestureRecognizer(target: self, action: #selector(close))
  74. tapGesture.delegate = self
  75. addGestureRecognizer(tapGesture)
  76. backgroundColor = .clear
  77. selectedBack = selectedDate
  78. if screenHeight == 812 {
  79. baseViewHeight = 49 + 30 + 6 * calendarItemWH + 6 * 8 + 34
  80. }
  81. baseView = UIView(frame: CGRect(x: 0, y: screenHeight, width: screenWidth, height: baseViewHeight))
  82. baseView.transform = CGAffineTransform.identity
  83. self.addSubview(baseView)
  84. baseView.backgroundColor = UIColor.hexInt(0xF5F6F5)
  85. let date = Date()
  86. let year = date.year
  87. let month = date.month
  88. let day = date.day
  89. currentDate = date.formatterDate(formatter: "YYYY-MM-dd")
  90. endDate = date.formatterDate(formatter: "YYYY-MM-dd")
  91. currentTime = date.formatterDate(formatter: "HH:mm")
  92. dateBtn = UIButton(frame: CGRect(x: 10, y: 0, width: 120, height: 49))
  93. dateBtn.setTitle("\(year)年\(month)月\(day)日", for: UIControl.State.normal)
  94. dateBtn.titleLabel?.font = UIFont.systemFont(ofSize: 14)
  95. dateBtn.setTitleColor(.darkGray, for: UIControl.State.selected)
  96. dateBtn.setTitleColor(.gray, for: UIControl.State.normal)
  97. dateBtn.isSelected = true
  98. dateBtn.addTarget(self, action: #selector(clickTimeBtn(_:)), for: UIControl.Event.touchUpInside)
  99. baseView.addSubview(dateBtn)
  100. endDateBtn = UIButton(frame: CGRect(x: dateBtn.x + dateBtn.width + 10, y: dateBtn.y, width: 120, height: dateBtn.height))
  101. endDateBtn.setTitle("\(year)年\(month)月\(day)日", for: UIControl.State.normal)
  102. endDateBtn.titleLabel?.font = UIFont.systemFont(ofSize: 14)
  103. endDateBtn.setTitleColor(.darkGray, for: UIControl.State.selected)
  104. endDateBtn.setTitleColor(.gray, for: UIControl.State.normal)
  105. endDateBtn.isSelected = false
  106. endDateBtn.addTarget(self, action: #selector(clickTimeBtn(_:)), for: UIControl.Event.touchUpInside)
  107. baseView.addSubview(endDateBtn)
  108. timeBtn = UIButton(frame: CGRect(x: dateBtn.x + dateBtn.width + 10, y: dateBtn.y, width: 54, height: dateBtn.height))
  109. timeBtn.setTitle(currentTime, for: UIControl.State.normal)
  110. timeBtn.titleLabel?.font = UIFont.systemFont(ofSize: 17)
  111. timeBtn.setTitleColor(.darkGray, for: UIControl.State.selected)
  112. timeBtn.setTitleColor(.gray, for: UIControl.State.normal)
  113. timeBtn.isSelected = false
  114. timeBtn.addTarget(self, action: #selector(clickTimeBtn(_:)), for: UIControl.Event.touchUpInside)
  115. baseView.addSubview(timeBtn)
  116. let okBtn = UIButton(type: UIButton.ButtonType.system)
  117. okBtn.frame = CGRect(x: screenWidth - 10 - 48, y: dateBtn.y, width: 48, height: dateBtn.height)
  118. okBtn.setTitle("确定", for: UIControl.State.normal)
  119. okBtn.titleLabel?.font = UIFont.systemFont(ofSize: 17)
  120. okBtn.addTarget(self, action: #selector(clickOKBtn), for: UIControl.Event.touchUpInside)
  121. baseView.addSubview(okBtn)
  122. cursorView = UIView(frame: CGRect(x: dateBtn.x, y: dateBtn.height - 2, width: dateBtn.width, height: 2))
  123. cursorView.backgroundColor = UIColor.hexInt(0x7D7F82)
  124. baseView.addSubview(cursorView)
  125. baseView.sendSubviewToBack(cursorView)
  126. }
  127. required init?(coder aDecoder: NSCoder) {
  128. fatalError("init(coder:) has not been implemented")
  129. }
  130. // MARK: - CalendarViewDelegate
  131. @objc func clickTimeBtn(_ button: UIButton) {
  132. if !button.isSelected {
  133. button.isSelected = true
  134. if button == dateBtn {
  135. timeBtn.isSelected = false
  136. endDateBtn.isSelected = false
  137. let rect = CGRect(x: dateBtn.x, y: dateBtn.height - 2, width: dateBtn.width, height: 2)
  138. UIView.animate(withDuration: 0.33) {
  139. self.cursorView.frame = rect
  140. if self.calendarPickerStyle == .dateTimePicker {
  141. self.calendarView.x = 0
  142. self.timePickerView.x = self.baseView.width
  143. }
  144. self.calendarView.setupDefaultShowDate(defaultDate: self.currentDate.toDate(formatter: "YYYY-MM-dd"))
  145. }
  146. } else if button == endDateBtn {
  147. let rect = CGRect(x: endDateBtn.x, y: endDateBtn.height - 2, width: endDateBtn.width, height: 2)
  148. dateBtn.isSelected = false
  149. timeBtn.isSelected = false
  150. UIView.animate(withDuration: 0.33) {
  151. self.cursorView.frame = rect
  152. self.calendarView.setupDefaultShowDate(defaultDate: self.endDate.toDate(formatter: "YYYY-MM-dd"))
  153. }
  154. } else {
  155. if !baseView.subviews.contains(timePickerView) {
  156. self.baseView.addSubview(self.timePickerView)
  157. }
  158. dateBtn.isSelected = false
  159. endDateBtn.isSelected = false
  160. let rect = CGRect(x: timeBtn.x, y: timeBtn.height - 2, width: timeBtn.width, height: 2)
  161. UIView.animate(withDuration: 0.33) {
  162. self.cursorView.frame = rect
  163. self.calendarView.x = -self.baseView.width
  164. self.timePickerView.x = 0
  165. }
  166. }
  167. }
  168. }
  169. // MARK: - CalendarViewDelegate
  170. func didSelectedDate(selecteDate: Date) {
  171. let selectedYear = selecteDate.year
  172. let selectedMonth = selecteDate.month
  173. let selectedDay = selecteDate.day
  174. if dateBtn.isSelected {
  175. currentDate = selecteDate.formatterDate(formatter: "YYYY-MM-dd")
  176. dateBtn.setTitle("\(selectedYear)年\(selectedMonth)月\(selectedDay)日", for: UIControl.State.normal)
  177. if self.calendarPickerStyle == .dateTimePicker {
  178. if !baseView.subviews.contains(timePickerView) {
  179. self.baseView.addSubview(self.timePickerView)
  180. }
  181. let rect = CGRect(x: timeBtn.x, y: timeBtn.height - 2, width: timeBtn.width, height: 2)
  182. dateBtn.isSelected = false
  183. timeBtn.isSelected = true
  184. UIView.animate(withDuration: 0.33) {
  185. self.cursorView.frame = rect
  186. self.calendarView.x = -self.baseView.width
  187. self.timePickerView.x = 0
  188. }
  189. }else if self.calendarPickerStyle == .dateIntervalPicker {
  190. let rect = CGRect(x: endDateBtn.x, y: endDateBtn.height - 2, width: endDateBtn.width, height: 2)
  191. dateBtn.isSelected = false
  192. endDateBtn.isSelected = true
  193. UIView.animate(withDuration: 0.33) {
  194. self.cursorView.frame = rect
  195. self.calendarView.setupDefaultShowDate(defaultDate: self.endDate.toDate(formatter: "YYYY-MM-dd"))
  196. }
  197. }
  198. }else if endDateBtn.isSelected {
  199. endDate = selecteDate.formatterDate(formatter: "YYYY-MM-dd")
  200. endDateBtn.setTitle("\(selectedYear)年\(selectedMonth)月\(selectedDay)日", for: UIControl.State.normal)
  201. }
  202. }
  203. // MARK: - TimePickerViewDelegate
  204. func selectedTime(time: String) {
  205. timeBtn.setTitle(time, for: UIControl.State.normal)
  206. currentTime = time
  207. }
  208. private func setupDefaultDate(date: Date, endDate: Date = Date()) {
  209. if self.calendarPickerStyle == .dateTimePicker {
  210. self.timePickerView.datePicker.setDate(date, animated: true)
  211. self.selectedTime(time: date.formatterDate(formatter: "HH:mm"))
  212. }else if self.calendarPickerStyle == .dateIntervalPicker {
  213. let selectedYear = endDate.year
  214. let selectedMonth = endDate.month
  215. let selectedDay = endDate.day
  216. self.endDate = endDate.formatterDate(formatter: "YYYY-MM-dd")
  217. self.endDateBtn.setTitle("\(selectedYear)年\(selectedMonth)月\(selectedDay)日", for: UIControl.State.normal)
  218. }
  219. self.calendarView.setupDefaultShowDate(defaultDate: date)
  220. self.didSelectedDate(selecteDate: date)
  221. }
  222. /// 显示
  223. open func showPickerWithDefault(defaultDate: Date = Date(), endDate: Date = Date()) {
  224. keyWindow?.addSubview(self)
  225. keyWindow?.bringSubviewToFront(self)
  226. self.setupDefaultDate(date: defaultDate, endDate: endDate)
  227. UIView.animate(withDuration: 0.21, animations: {
  228. self.baseView.transform = CGAffineTransform(translationX: 0, y: -baseViewHeight)
  229. self.backgroundColor = UIColor.black.withAlphaComponent(0.4)
  230. }, completion: { (finish: Bool) in
  231. self.baseView.addSubview(self.calendarView)
  232. } )
  233. }
  234. /// 显示
  235. open func show() {
  236. keyWindow?.addSubview(self)
  237. keyWindow?.bringSubviewToFront(self)
  238. UIView.animate(withDuration: 0.21, animations: {
  239. self.baseView.transform = CGAffineTransform(translationX: 0, y: -baseViewHeight)
  240. self.backgroundColor = UIColor.black.withAlphaComponent(0.4)
  241. }, completion: { (finish: Bool) in
  242. self.baseView.addSubview(self.calendarView)
  243. } )
  244. }
  245. @objc private func clickOKBtn() {
  246. if self.calendarPickerStyle == .dateTimePicker {
  247. selectedBack!(currentDate + " " + currentTime)
  248. } else if self.calendarPickerStyle == .datePicker {
  249. selectedBack!(currentDate)
  250. } else {
  251. selectedBack!(currentDate + " " + endDate)
  252. }
  253. close()
  254. }
  255. @objc private func close() {
  256. UIView.animate(withDuration: 0.15, animations: {
  257. self.baseView.transform = CGAffineTransform.identity
  258. self.backgroundColor = .clear
  259. }) { (finish: Bool) in
  260. self.removeFromSuperview()
  261. }
  262. }
  263. func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldReceive touch: UITouch) -> Bool {
  264. if touch.view != self {
  265. return false
  266. }
  267. return true
  268. }
  269. }
  270. //****************************< 以下日历模块 >************************************
  271. // MARK: - 日历模块
  272. /// 日历
  273. protocol CalendarViewDelegate:NSObjectProtocol {
  274. func didSelectedDate(selecteDate: Date)
  275. }
  276. class CalendarView: UIView, UICollectionViewDelegate, UICollectionViewDataSource, UIScrollViewDelegate {
  277. var collectionView: UICollectionView!
  278. var currentDate : Date! = Date()
  279. var yearLbl: UILabel!
  280. var selectedDay: Date! = Date()
  281. weak var delegate: CalendarViewDelegate?
  282. override init(frame: CGRect) {
  283. super.init(frame: frame)
  284. backgroundColor = .white
  285. creatWeekTitle()
  286. collectionView = UICollectionView(frame: CGRect(x: 10, y: 30, width: self.width - 20, height: self.height - 30), collectionViewLayout: CalendarLayout())
  287. collectionView.delegate = self
  288. collectionView.dataSource = self
  289. collectionView.backgroundColor = .clear
  290. collectionView.showsVerticalScrollIndicator = false
  291. collectionView.isPagingEnabled = true
  292. addSubview(collectionView)
  293. yearLbl = UILabel(frame: collectionView.bounds)
  294. yearLbl.textColor = UIColor.hexInt(0xE9EDF2)
  295. yearLbl.font = UIFont(name: "DB LCD Temp", size: 110)
  296. yearLbl.textAlignment = NSTextAlignment.center
  297. yearLbl.adjustsFontSizeToFitWidth = true
  298. yearLbl.text = "\(currentDate.year)"
  299. addSubview(yearLbl)
  300. sendSubviewToBack(yearLbl)
  301. collectionView.register(CalendarCell.self, forCellWithReuseIdentifier: "cell")
  302. collectionView.setContentOffset(CGPoint(x: 0, y: (self.height - 30)*1), animated: false)
  303. }
  304. required init?(coder aDecoder: NSCoder) {
  305. fatalError("init(coder:) has not been implemented")
  306. }
  307. func creatWeekTitle() {
  308. let titles = ["日", "一", "二", "三", "四", "五", "六"]
  309. for i in 0..<titles.count {
  310. let label = UILabel(frame: CGRect(x: 10 + (((screenWidth - 20 - 6 * 8) / 7) * CGFloat(i)) + 8 * CGFloat(i), y: 0, width: (screenWidth - 20 - 6 * 8) / 7, height: 30))
  311. label.textAlignment = .center
  312. label.font = UIFont.systemFont(ofSize: 13)
  313. label.text = titles[i]
  314. addSubview(label)
  315. }
  316. }
  317. func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
  318. return 42*3
  319. }
  320. func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
  321. let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! CalendarCell
  322. let daysInThisMonth = currentDate.totalDaysInThisMonth
  323. let firstWeekDay = currentDate.firstWeekDayInThisMonth
  324. let currentMonth = currentDate.month
  325. let currentYear = currentDate.year
  326. let lastMonthDate = currentDate.lastMonth
  327. let lastMonth = lastMonthDate.month
  328. let daysInLastMonth = lastMonthDate.totalDaysInThisMonth
  329. let nextMonthDate = currentDate.nextMonth
  330. let nextMonth = nextMonthDate.month
  331. let daysInNextMonth = nextMonthDate.totalDaysInThisMonth
  332. let i = (indexPath.row - 42)
  333. if i < firstWeekDay {
  334. let lastDay = (daysInLastMonth - firstWeekDay + 1 + i)
  335. if lastDay == 1 {
  336. cell.text = "\(lastMonth)月"
  337. } else if lastDay <= 0 {
  338. let doubleLastMonthDate = lastMonthDate.lastMonth
  339. let daysInDoubleLastMonth = doubleLastMonthDate.totalDaysInThisMonth
  340. cell.text = "\(daysInDoubleLastMonth + lastDay)"
  341. } else {
  342. cell.text = String(lastDay)
  343. }
  344. cell.textColor = .lightGray
  345. cell.backgroundColor = UIColor.clear
  346. cell.isUserInteractionEnabled = false
  347. } else if i > firstWeekDay + daysInThisMonth - 1 {
  348. let nextCurrentDay = (i - firstWeekDay - daysInThisMonth + 1)
  349. if nextCurrentDay <= daysInNextMonth {
  350. if nextCurrentDay == 1 {
  351. cell.text = String(nextMonth) + "月"
  352. } else {
  353. cell.text = String(nextCurrentDay)
  354. }
  355. } else {
  356. let doubleNextMonthCurrentDay = (nextCurrentDay - daysInNextMonth)
  357. if doubleNextMonthCurrentDay == 1 {
  358. if nextMonth == 12 {
  359. cell.text = "1月"
  360. } else {
  361. cell.text = "\(nextMonth + 1)月"
  362. }
  363. } else {
  364. cell.text = "\(doubleNextMonthCurrentDay)"
  365. }
  366. }
  367. cell.textColor = .lightGray
  368. cell.backgroundColor = UIColor.clear
  369. cell.isUserInteractionEnabled = false
  370. } else {
  371. let currentDay = (i - firstWeekDay + 1)
  372. if currentDay == Date().day
  373. && currentMonth == Date().month
  374. && currentYear == Date().year {
  375. cell.text = String(currentDay)
  376. if currentDay == selectedDay.day
  377. && currentMonth == selectedDay.month
  378. && currentYear == selectedDay.year {
  379. cell.backgroundColor = UIColor.hexInt(0x7D7F82)
  380. lastSelected = indexPath.row // 记录选中的item
  381. cell.textColor = .white
  382. } else {
  383. cell.backgroundColor = UIColor.hexInt(0xE9F3FE)
  384. cell.textColor = UIColor.hexInt(0x297DFF)
  385. }
  386. } else {
  387. if currentDay == 1 {
  388. cell.text = String(currentMonth) + "月"
  389. if currentDay == selectedDay.day
  390. && currentMonth == selectedDay.month
  391. && currentYear == selectedDay.year {
  392. cell.textColor = .white
  393. cell.backgroundColor = UIColor.hexInt(0x7D7F82)
  394. lastSelected = indexPath.row // 记录选中的item
  395. } else {
  396. cell.textColor = UIColor.hexInt(0x297DFF)
  397. cell.backgroundColor = UIColor.clear
  398. }
  399. } else {
  400. if currentDay == selectedDay.day
  401. && currentMonth == selectedDay.month
  402. && currentYear == selectedDay.year {
  403. cell.textColor = .white
  404. cell.backgroundColor = UIColor.hexInt(0x7D7F82)
  405. lastSelected = indexPath.row // 记录选中的item
  406. } else {
  407. cell.textColor = .black
  408. cell.backgroundColor = UIColor.clear
  409. }
  410. cell.text = String(currentDay)
  411. }
  412. }
  413. cell.isUserInteractionEnabled = true
  414. }
  415. return cell
  416. }
  417. /// 上次选中的日期, 默认初始值为当前日期
  418. var lastSelected: NSInteger! = (42 + Date().firstWeekDayInThisMonth - 1 + Date().day)
  419. func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
  420. // 计算选中的日期
  421. let firstWeekDay = currentDate.firstWeekDayInThisMonth
  422. let clickDay = (indexPath.row - 42 - firstWeekDay + 1)
  423. var dateComponents = DateComponents()
  424. dateComponents.day = -currentDate.day + clickDay
  425. let clickDate = Calendar.current.date(byAdding: dateComponents, to: currentDate)
  426. selectedDay = clickDate
  427. delegate?.didSelectedDate(selecteDate: clickDate!)
  428. // 刷新上次选中和当前选中的items
  429. var arr = [IndexPath]()
  430. if lastSelected != indexPath.row {
  431. arr.append(IndexPath(item: lastSelected, section: 0))
  432. }
  433. arr.append(IndexPath(item: indexPath.row, section: 0))
  434. collectionView.reloadItems(at: arr)
  435. //放到 loadItem 那边去
  436. // lastSelected = indexPath.row // 记录选中的item
  437. }
  438. func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
  439. let direction = lroundf(Float(collectionView.contentOffset.y / collectionView.height))
  440. if direction == 0 {
  441. self.currentDate = self.currentDate.lastMonth
  442. reseData()
  443. }
  444. if direction == 2 {
  445. self.currentDate = self.currentDate.nextMonth
  446. reseData()
  447. }
  448. }
  449. func reseData() {
  450. collectionView.setContentOffset(CGPoint(x: 0, y: (self.height - 30)*1), animated: false)
  451. collectionView.reloadData()
  452. self.yearLbl.text = "\(self.currentDate.year)"
  453. }
  454. func setupDefaultShowDate(defaultDate: Date) {
  455. self.currentDate = defaultDate
  456. self.selectedDay = defaultDate
  457. self.reseData()
  458. }
  459. }
  460. // MARK: - CalendarLayout
  461. /// 定义 UICollectionViewFlowLayout
  462. class CalendarLayout: UICollectionViewFlowLayout {
  463. override init() {
  464. super.init()
  465. itemSize = CGSize(width: calendarItemWH, height: calendarItemWH)
  466. scrollDirection = .vertical
  467. minimumLineSpacing = 8
  468. minimumInteritemSpacing = 8
  469. sectionInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
  470. }
  471. required init?(coder aDecoder: NSCoder) {
  472. fatalError("init(coder:) has not been implemented")
  473. }
  474. }
  475. // MARK: - 日历单元 cell
  476. /// 日历单元 cell
  477. class CalendarCell: UICollectionViewCell {
  478. open var text: String! {
  479. set {
  480. self.textLbl.text = newValue
  481. }
  482. get {
  483. return self.text
  484. }
  485. }
  486. open var textColor: UIColor! {
  487. set {
  488. self.textLbl.textColor = newValue
  489. }
  490. get {
  491. return self.textColor
  492. }
  493. }
  494. private lazy var textLbl: UILabel = {
  495. let label = UILabel(frame: self.bounds)
  496. label.backgroundColor = .clear
  497. label.textAlignment = .center
  498. label.adjustsFontSizeToFitWidth = true
  499. if #available(iOS 10.0, *) {
  500. label.adjustsFontForContentSizeCategory = true
  501. }
  502. return label
  503. }()
  504. override init(frame: CGRect) {
  505. super.init(frame: frame)
  506. backgroundColor = .clear
  507. addSubview(textLbl)
  508. }
  509. required init?(coder aDecoder: NSCoder) {
  510. fatalError("init(coder:) has not been implemented")
  511. }
  512. }
  513. //***************************< 以下时间选择模块 >**********************************
  514. // MARK: - 时间选择模块
  515. protocol TimePickerViewDelegate:NSObjectProtocol {
  516. func selectedTime(time: String)
  517. }
  518. class TimePickerView: UIView {
  519. var datePicker: UIDatePicker!
  520. weak var delegate: TimePickerViewDelegate?
  521. override init(frame: CGRect) {
  522. super.init(frame: frame)
  523. backgroundColor = .white
  524. datePicker = UIDatePicker(frame: frame)
  525. datePicker.centerY = self.height / 2
  526. datePicker.locale = Locale(identifier: "zh")
  527. datePicker.datePickerMode = UIDatePicker.Mode.time
  528. datePicker.addTarget(self, action: #selector(datePickerValueChange(_:)), for: UIControl.Event.valueChanged)
  529. addSubview(datePicker)
  530. }
  531. @objc func datePickerValueChange(_ datePicker: UIDatePicker) {
  532. let date = datePicker.date
  533. let dateFormatter = DateFormatter()
  534. dateFormatter.dateFormat = "HH:mm"
  535. let time = dateFormatter.string(from: date)
  536. delegate?.selectedTime(time: time)
  537. }
  538. required init?(coder aDecoder: NSCoder) {
  539. fatalError("init(coder:) has not been implemented")
  540. }
  541. }