QCalendarPicker.swift 24 KB

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