MCDatePikerDelegate.swift 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816
  1. //
  2. // MCDatePikerDelegate.swift
  3. // HCQuanfangtong
  4. //
  5. // Created by Apple on 2022/5/10.
  6. // Copyright © 2022 Jyp. All rights reserved.
  7. //
  8. import Foundation
  9. import SwiftDate
  10. protocol MCDatePikerDelegate : UIPickerViewDelegate, UIPickerViewDataSource{
  11. /// 样式
  12. var style : MCDatePikerView! {get set}
  13. /// 最小值
  14. var minDate : Date? {get set}
  15. /// 最大值
  16. var maxDate : Date? {get set}
  17. /// 当前日期
  18. var defaultDate : Date! {get set}
  19. /// 当前选择的月份有多少天
  20. var monthOfDays : Int! {get set}
  21. //MARK: - 被选中的年月日时分秒
  22. var year : Int? {get set}
  23. var month : Int? {get set}
  24. var day : Int? {get set}
  25. var hour : Int? {get set}
  26. var minute : Int? {get set}
  27. var seconds : Int? {get set}
  28. }
  29. extension MCDatePikerDelegate{
  30. /// 最终的日期
  31. var finallyDate : Date{
  32. get{
  33. Date.init(year: year ?? 1, month: month ?? 1, day: day ?? 1, hour: hour ?? 0, minute: minute ?? 0, second: seconds ?? 0)
  34. }
  35. }
  36. /// 可以选择的样式
  37. var pikerRowStyle : [NSAttributedString.Key : Any]{
  38. get{
  39. [NSAttributedString.Key.foregroundColor: self.style.delegateForPikerTitleColor(), NSAttributedString.Key.font: self.style.delegateForPikerTitleFont()]
  40. }
  41. }
  42. /// 不可以选择的样式
  43. var pikerDisableStyle : [NSAttributedString.Key : Any]{
  44. get{
  45. [NSAttributedString.Key.foregroundColor: self.style.delegateForPikerTitleDisableColor(), NSAttributedString.Key.font: self.style.delegateForPikerTitleDisableFont()]
  46. }
  47. }
  48. /// 当前年份是否可以被选中
  49. /// - Parameter row: <#row description#>
  50. /// - Returns: <#description#>
  51. func canBeSelectedOfYear(row: Int) -> Bool{
  52. if (minDate.isNotNil && row + 1 < minDate!.year) || (maxDate.isNotNil && row + 1 > maxDate!.year){
  53. return false
  54. }
  55. return true
  56. }
  57. /// 当前月份是否可以被选中
  58. /// - Parameter row: <#row description#>
  59. /// - Returns: <#description#>
  60. func canBeSelectedOfMonth(row: Int) -> Bool{
  61. if minDate.isNil && maxDate.isNil{
  62. return true
  63. }
  64. let adjustMonth = row % 12 + 1
  65. if minDate?.year ?? 0 == (maxDate?.year ?? 0){
  66. //年份相同
  67. if adjustMonth < (minDate?.month ?? 0){
  68. return false
  69. }
  70. if adjustMonth > (maxDate?.month ?? 0){
  71. return false
  72. }
  73. }
  74. else{
  75. //年分不同
  76. if year == (minDate?.year ?? 0) && adjustMonth < (minDate?.month ?? 0){
  77. return false
  78. }
  79. if year == (maxDate?.year ?? 0) && adjustMonth > (maxDate?.month ?? 0){
  80. return false
  81. }
  82. }
  83. return true
  84. }
  85. /// 当前日期是否可以被选中
  86. /// - Parameter row: <#row description#>
  87. /// - Returns: <#description#>
  88. func canBeSelectedOfDay(row: Int) -> Bool{
  89. if minDate.isNil && maxDate.isNil{
  90. return true
  91. }
  92. //当前月份有多少天
  93. let daysOfMonth : Int = monthOfDays
  94. //当前row 对应的月份
  95. let adjustDay = row % daysOfMonth + 1
  96. if minDate?.year ?? 0 == (maxDate?.year ?? 0) && minDate?.month ?? 0 == (maxDate?.month ?? 0){
  97. //年相同 月相同
  98. if adjustDay < (minDate?.day ?? 0){
  99. return false
  100. }
  101. if adjustDay > (maxDate?.day ?? 0){
  102. return false
  103. }
  104. return true
  105. }
  106. if year == (minDate?.year ?? 0) && month == (minDate?.month ?? 0) && adjustDay < (minDate?.day ?? 0){
  107. return false
  108. }
  109. if year == (maxDate?.year ?? 0) && month == (maxDate?.month ?? 0) && adjustDay > (maxDate?.day ?? 0){
  110. return false
  111. }
  112. return true
  113. }
  114. /// 当前小时 是否可以被选中
  115. /// - Parameter row: <#row description#>
  116. /// - Returns: <#description#>
  117. func canBeSelectedOfHour(row: Int) -> Bool{
  118. if minDate.isNil && maxDate.isNil{
  119. return true
  120. }
  121. let adjustHour = row % 24
  122. if style.dateType == .HHmm || style.dateType == .HHmmss{
  123. if minDate.isNotNil && adjustHour < (minDate?.hour ?? 0){
  124. return false
  125. }
  126. if maxDate.isNotNil && adjustHour > (maxDate?.hour ?? 0){
  127. return false
  128. }
  129. return true
  130. }
  131. //快速判断
  132. if minDate.isNotNil && maxDate.isNotNil{
  133. if adjustHour >= (minDate?.hour ?? 0) && adjustHour <= (maxDate?.hour ?? 0){
  134. return true
  135. }
  136. }
  137. else{
  138. if minDate.isNotNil && adjustHour >= (minDate?.hour ?? 0){
  139. return true
  140. }
  141. if maxDate.isNotNil && adjustHour <= (maxDate?.hour ?? 0){
  142. return true
  143. }
  144. }
  145. //其他情况
  146. if minDate?.year ?? 0 == (maxDate?.year ?? 0) && minDate?.month ?? 0 == (maxDate?.month ?? 0) && minDate?.day ?? 0 == (maxDate?.day ?? 0){
  147. //年相同 月相同 日相同
  148. if adjustHour < (minDate?.hour ?? 0){
  149. return false
  150. }
  151. if adjustHour > (maxDate?.hour ?? 0){
  152. return false
  153. }
  154. return true
  155. }
  156. if year == (minDate?.year ?? 0) && month == (minDate?.month ?? 0) && day == (minDate?.day ?? 0) && adjustHour < (minDate?.hour ?? 0){
  157. return false
  158. }
  159. if year == (maxDate?.year ?? 0) && month == (maxDate?.month ?? 0) && day == (maxDate?.day ?? 0) && adjustHour > (maxDate?.hour ?? 0){
  160. return false
  161. }
  162. return true
  163. }
  164. /// 当前分钟 是否可以被选中
  165. /// - Parameter row: <#row description#>
  166. /// - Returns: <#description#>
  167. func canBeSelectedOfMinute(row: Int) -> Bool{
  168. if minDate.isNil && maxDate.isNil{
  169. return true
  170. }
  171. let adjustMinute = row % 60
  172. if style.dateType == .HHmm || style.dateType == .HHmmss{
  173. if minDate?.hour ?? 0 == (maxDate?.hour ?? 0){
  174. //小时相同
  175. if adjustMinute < (minDate?.minute ?? 0){
  176. return false
  177. }
  178. if adjustMinute > (maxDate?.minute ?? 0){
  179. return false
  180. }
  181. }
  182. else{
  183. //小时不同
  184. if hour == (minDate?.hour ?? 0) && adjustMinute < (minDate?.minute ?? 0){
  185. return false
  186. }
  187. if hour == (maxDate?.hour ?? 0) && adjustMinute > (maxDate?.minute ?? 0){
  188. return false
  189. }
  190. }
  191. return true
  192. }
  193. //快速判断
  194. if minDate.isNotNil && maxDate.isNotNil{
  195. if adjustMinute >= (minDate?.minute ?? 0) && adjustMinute <= (maxDate?.minute ?? 0){
  196. return true
  197. }
  198. }
  199. else{
  200. if minDate.isNotNil && adjustMinute >= (minDate?.minute ?? 0){
  201. return true
  202. }
  203. if maxDate.isNotNil && adjustMinute <= (maxDate?.minute ?? 0){
  204. return true
  205. }
  206. }
  207. //其他情况
  208. if minDate?.year ?? 0 == (maxDate?.year ?? 0) && minDate?.month ?? 0 == (maxDate?.month ?? 0) && minDate?.day ?? 0 == (maxDate?.day ?? 0) && minDate?.hour ?? 0 == (maxDate?.hour ?? 0){
  209. //年 月 日 时 相同
  210. if adjustMinute < (minDate?.minute ?? 0){
  211. return false
  212. }
  213. if adjustMinute > (maxDate?.minute ?? 0){
  214. return false
  215. }
  216. return true
  217. }
  218. if year == (minDate?.year ?? 0) && month == (minDate?.month ?? 0) && day == (minDate?.day ?? 0) && hour == (minDate?.hour ?? 0) && adjustMinute < (minDate?.minute ?? 0){
  219. return false
  220. }
  221. if year == (maxDate?.year ?? 0) && month == (maxDate?.month ?? 0) && day == (maxDate?.day ?? 0) && hour == (maxDate?.hour ?? 0) && adjustMinute > (maxDate?.minute ?? 0){
  222. return false
  223. }
  224. return true
  225. }
  226. /// 当前秒 是否可以被选中
  227. /// - Parameter row: <#row description#>
  228. /// - Returns: <#description#>
  229. func canBeSelectedOfSecond(row: Int) -> Bool{
  230. if minDate.isNil && maxDate.isNil{
  231. return true
  232. }
  233. let adjustSeconds = row % 60
  234. if style.dateType == .HHmm || style.dateType == .HHmmss{
  235. if minDate?.hour ?? 0 == (maxDate?.hour ?? 0) && minDate?.minute ?? 0 == (maxDate?.minute ?? 0){
  236. //时 分 相同
  237. if adjustSeconds < (minDate?.second ?? 0){
  238. return false
  239. }
  240. if adjustSeconds > (maxDate?.second ?? 0){
  241. return false
  242. }
  243. return true
  244. }
  245. if hour == (minDate?.hour ?? 0) && minute == (minDate?.minute ?? 0) && adjustSeconds < (minDate?.second ?? 0){
  246. return false
  247. }
  248. if hour == (maxDate?.hour ?? 0) && minute == (maxDate?.minute ?? 0) && adjustSeconds > (maxDate?.second ?? 0){
  249. return false
  250. }
  251. return true
  252. }
  253. //快速判断
  254. if minDate.isNotNil && maxDate.isNotNil{
  255. if adjustSeconds >= (minDate?.second ?? 0) && adjustSeconds <= (maxDate?.second ?? 0){
  256. return true
  257. }
  258. }
  259. else{
  260. if minDate.isNotNil && adjustSeconds >= (minDate?.second ?? 0){
  261. return true
  262. }
  263. if maxDate.isNotNil && adjustSeconds <= (maxDate?.second ?? 0){
  264. return true
  265. }
  266. }
  267. //其他情况
  268. if minDate?.year ?? 0 == (maxDate?.year ?? 0) && minDate?.month ?? 0 == (maxDate?.month ?? 0) && minDate?.day ?? 0 == (maxDate?.day ?? 0) && minDate?.hour ?? 0 == (maxDate?.hour ?? 0) && minDate?.minute ?? 0 == (maxDate?.minute ?? 0){
  269. //年 月 日 时 分 相同
  270. if adjustSeconds < (minDate?.second ?? 0){
  271. return false
  272. }
  273. if adjustSeconds > (maxDate?.second ?? 0){
  274. return false
  275. }
  276. return true
  277. }
  278. if year == (minDate?.year ?? 0) && month == (minDate?.month ?? 0) && day == (minDate?.day ?? 0) && hour == (minDate?.hour ?? 0) && minute == (minDate?.minute ?? 0) && adjustSeconds < (minDate?.second ?? 0){
  279. return false
  280. }
  281. if year == (maxDate?.year ?? 0) && month == (maxDate?.month ?? 0) && day == (maxDate?.day ?? 0) && hour == (maxDate?.hour ?? 0) && minute == (maxDate?.minute ?? 0) && adjustSeconds > (maxDate?.second ?? 0){
  282. return false
  283. }
  284. return true
  285. }
  286. }
  287. extension MCDatePikerDelegate{
  288. /// 获取row 对应的最适合的月份
  289. /// - Parameters:
  290. /// - delegate: <#delegate description#>
  291. /// - row: 当前components 的row
  292. /// - targetMonth: 目标月份,想要调整到多少月
  293. /// - Returns: 返回最适合的row
  294. func getAdjustMonth(row: Int, targetMonth: Int? = nil) -> Int{
  295. guard let year = year else{
  296. return row
  297. }
  298. let adjustMonth = row % 12 + 1
  299. let fullYearOfMonths = (row + 1) / 12 * 12
  300. if targetMonth.isNotNil{
  301. return fullYearOfMonths + targetMonth! - 1
  302. }
  303. if minDate.isNil && maxDate.isNil{
  304. return row
  305. }
  306. if minDate?.year ?? 0 == (maxDate?.year ?? 0){
  307. //年份相同
  308. if adjustMonth < (minDate?.month ?? 0){
  309. return fullYearOfMonths + (minDate?.month ?? 0) - 1
  310. }
  311. if adjustMonth > (maxDate?.month ?? 0){
  312. return fullYearOfMonths + (maxDate?.month ?? 0) - 1
  313. }
  314. }
  315. else{
  316. //年分不同
  317. if year == (minDate?.year ?? 0) && adjustMonth < (minDate?.month ?? 0){
  318. return fullYearOfMonths + (minDate?.month ?? 0) - 1
  319. }
  320. if year == (maxDate?.year ?? 0) && adjustMonth > (maxDate?.month ?? 0){
  321. return fullYearOfMonths + (maxDate?.month ?? 0) - 1
  322. }
  323. }
  324. return row
  325. }
  326. /// 获取row 下对应的最适合的天数
  327. /// - Parameters:
  328. /// - delegate: <#delegate description#>
  329. /// - row: 当前components的row
  330. /// - beforeDay: 之前选中的天
  331. /// - targetDay: 目标天数
  332. /// - Returns: 返回最适合的天数
  333. func getAdjusetDay(row: Int, beforeDay: Int? = nil, targetDay: Int? = nil) -> Int{
  334. var adjustRow = self.getBeChangedDay(row: row, beforeDay: beforeDay, targetDay: targetDay)
  335. if beforeDay.isNil{
  336. return adjustRow
  337. }
  338. if adjustRow != row{
  339. //天数
  340. if adjustRow - row > monthOfDays{
  341. adjustRow = adjustRow - (adjustRow - row)/monthOfDays * monthOfDays
  342. }
  343. else if row - adjustRow < monthOfDays{
  344. adjustRow = adjustRow + (row - adjustRow)/monthOfDays * monthOfDays
  345. }
  346. adjustRow = row - (row % monthOfDays + 1 - (adjustRow % monthOfDays + 1))
  347. }
  348. return adjustRow
  349. }
  350. private func getBeChangedDay(row: Int, beforeDay: Int? = nil, targetDay: Int? = nil) -> Int{
  351. guard let year = year, let month = month else{
  352. return row
  353. }
  354. let daysOfMonth = monthOfDays!
  355. var fullMonthOfDays : Int
  356. if (row + 1) % daysOfMonth == 0{
  357. fullMonthOfDays = ((row + 1) / daysOfMonth - 1) * daysOfMonth
  358. }
  359. else{
  360. fullMonthOfDays = (row + 1) / daysOfMonth * daysOfMonth
  361. }
  362. if targetDay.isNotNil{
  363. //算当前year 和 month 下 天数
  364. if targetDay! > daysOfMonth{
  365. fatalError("数据有问题")
  366. }
  367. return fullMonthOfDays + targetDay! - 1
  368. }
  369. if minDate.isNil && maxDate.isNil{
  370. if beforeDay.isNil{
  371. return row
  372. }
  373. if beforeDay! <= daysOfMonth{
  374. return fullMonthOfDays + beforeDay! - 1
  375. }
  376. return fullMonthOfDays + daysOfMonth - 1
  377. }
  378. var adjustDay = 0
  379. if beforeDay.isNotNil{
  380. adjustDay = (beforeDay! > daysOfMonth ? daysOfMonth : beforeDay!)
  381. }
  382. else{
  383. adjustDay = row % daysOfMonth + 1
  384. }
  385. if minDate?.year ?? 0 == (maxDate?.year ?? 0) && minDate?.month ?? 0 == (maxDate?.month ?? 0){
  386. //年 月 相同
  387. if adjustDay < (minDate?.day ?? 0){
  388. return fullMonthOfDays + minDate!.day - 1
  389. }
  390. if adjustDay > (maxDate?.day ?? 0){
  391. return fullMonthOfDays + maxDate!.day - 1
  392. }
  393. return fullMonthOfDays + adjustDay - 1
  394. }
  395. if year == (minDate?.year ?? 0) && month == (minDate?.month ?? 0) && adjustDay < (minDate?.day ?? 0){
  396. return fullMonthOfDays + minDate!.day - 1
  397. }
  398. if year == (maxDate?.year ?? 0) && month == (maxDate?.month ?? 0) && adjustDay > (maxDate?.day ?? 0){
  399. return fullMonthOfDays + maxDate!.day - 1
  400. }
  401. return fullMonthOfDays + adjustDay - 1
  402. }
  403. /// 获取row 对应的最适合的小时
  404. /// - Parameters:
  405. /// - delegate: <#delegate description#>
  406. /// - row: 当前components 的row
  407. /// - targetHour: 目标月份,想要调整到多少月
  408. /// - Returns: 返回最适合的row
  409. func getAdjustHour(row: Int, targetHour: Int? = nil) -> Int{
  410. let fullDayOfHours = row / 24 * 24
  411. if targetHour.isNotNil{
  412. return fullDayOfHours + targetHour!
  413. }
  414. if minDate.isNil && maxDate.isNil{
  415. return row
  416. }
  417. let adjustHour = row % 24
  418. if style.dateType == .HHmm || style.dateType == .HHmmss{
  419. if minDate.isNotNil && adjustHour < (minDate?.hour ?? 0){
  420. return fullDayOfHours + (minDate?.hour ?? 0)
  421. }
  422. if maxDate.isNotNil && adjustHour > (maxDate?.hour ?? 0){
  423. return fullDayOfHours + (minDate?.hour ?? 0)
  424. }
  425. return row
  426. }
  427. guard let year = year, let month = month, let day = day else{
  428. return row
  429. }
  430. //快速判断
  431. if minDate.isNotNil && maxDate.isNotNil{
  432. if adjustHour >= (minDate?.hour ?? 0) && adjustHour <= (maxDate?.hour ?? 0){
  433. return row
  434. }
  435. }
  436. else{
  437. if minDate.isNotNil && adjustHour >= (minDate?.hour ?? 0){
  438. return row
  439. }
  440. if maxDate.isNotNil && adjustHour <= (maxDate?.hour ?? 0){
  441. return row
  442. }
  443. }
  444. //其他情况
  445. if minDate?.year ?? 0 == (maxDate?.year ?? 0) && minDate?.month ?? 0 == (maxDate?.month ?? 0) && minDate?.day ?? 0 == (maxDate?.day ?? 0){
  446. //年相同 月相同 日相同
  447. if adjustHour < (minDate?.hour ?? 0){
  448. return fullDayOfHours + (minDate?.hour ?? 0)
  449. }
  450. if adjustHour > (maxDate?.hour ?? 0){
  451. return fullDayOfHours + (maxDate?.hour ?? 0)
  452. }
  453. return row
  454. }
  455. if year == (minDate?.year ?? 0) && month == (minDate?.month ?? 0) && day == (minDate?.day ?? 0) && adjustHour < (minDate?.hour ?? 0){
  456. return fullDayOfHours + (minDate?.hour ?? 0)
  457. }
  458. if year == (maxDate?.year ?? 0) && month == (maxDate?.month ?? 0) && day == (maxDate?.day ?? 0) && adjustHour > (maxDate?.hour ?? 0){
  459. return fullDayOfHours + (maxDate?.hour ?? 0)
  460. }
  461. return row
  462. }
  463. /// 获取row 对应的最适合的分钟
  464. /// - Parameters:
  465. /// - row: <#row description#>
  466. /// - targetMinute: <#targetMinute description#>
  467. /// - Returns: <#description#>
  468. func getAdjustMinute(row: Int, targetMinute: Int? = nil) -> Int{
  469. let fullHourOfMinutes = row / 60 * 60
  470. if targetMinute.isNotNil{
  471. return fullHourOfMinutes + targetMinute!
  472. }
  473. if minDate.isNil && maxDate.isNil{
  474. return row
  475. }
  476. let adjustMinute = row % 60
  477. if style.dateType == .HHmm || style.dateType == .HHmmss{
  478. if minDate?.hour ?? 0 == (maxDate?.hour ?? 0){
  479. //小时相同
  480. if adjustMinute < (minDate?.minute ?? 0){
  481. return fullHourOfMinutes + (minDate?.minute ?? 0)
  482. }
  483. if adjustMinute > (maxDate?.minute ?? 0){
  484. return fullHourOfMinutes + (maxDate?.minute ?? 0)
  485. }
  486. }
  487. else{
  488. //小时不同
  489. if hour == (minDate?.hour ?? 0) && adjustMinute < (minDate?.minute ?? 0){
  490. return fullHourOfMinutes + (minDate?.minute ?? 0)
  491. }
  492. if hour == (maxDate?.hour ?? 0) && adjustMinute > (maxDate?.minute ?? 0){
  493. return fullHourOfMinutes + (maxDate?.minute ?? 0)
  494. }
  495. }
  496. return row
  497. }
  498. guard let year = year, let month = month, let day = day, let hour = hour else{
  499. return row
  500. }
  501. //快速判断
  502. if minDate.isNotNil && maxDate.isNotNil{
  503. if adjustMinute >= (minDate?.minute ?? 0) && adjustMinute <= (maxDate?.minute ?? 0){
  504. return row
  505. }
  506. }
  507. else{
  508. if minDate.isNotNil && adjustMinute >= (minDate?.minute ?? 0){
  509. return row
  510. }
  511. if maxDate.isNotNil && adjustMinute <= (maxDate?.minute ?? 0){
  512. return row
  513. }
  514. }
  515. if minDate?.year ?? 0 == (maxDate?.year ?? 0) && minDate?.month ?? 0 == (maxDate?.month ?? 0) && minDate?.day ?? 0 == (maxDate?.day ?? 0) && minDate?.hour ?? 0 == (maxDate?.hour ?? 0){
  516. //年 月 日 时 相同
  517. if adjustMinute < (minDate?.minute ?? 0){
  518. return fullHourOfMinutes + (minDate?.minute ?? 0)
  519. }
  520. if adjustMinute > (maxDate?.minute ?? 0){
  521. return fullHourOfMinutes + (maxDate?.minute ?? 0)
  522. }
  523. return row
  524. }
  525. if year == (minDate?.year ?? 0) && month == (minDate?.month ?? 0) && day == (minDate?.day ?? 0) && hour == (minDate?.hour ?? 0) && adjustMinute < (minDate?.minute ?? 0){
  526. return fullHourOfMinutes + (minDate?.minute ?? 0)
  527. }
  528. if year == (maxDate?.year ?? 0) && month == (maxDate?.month ?? 0) && day == (maxDate?.day ?? 0) && hour == (maxDate?.hour ?? 0) && adjustMinute > (maxDate?.minute ?? 0){
  529. return fullHourOfMinutes + (maxDate?.minute ?? 0)
  530. }
  531. return row
  532. }
  533. /// 获取row 对应的最适合的分钟
  534. /// - Parameters:
  535. /// - row: <#row description#>
  536. /// - targetSeconds: <#targetMinute description#>
  537. /// - Returns: <#description#>
  538. func getAdjustSeconds(row: Int, targetSeconds: Int? = nil) -> Int{
  539. let fullMinuteOfSeconds = row / 60 * 60
  540. if targetSeconds.isNotNil{
  541. return fullMinuteOfSeconds + targetSeconds!
  542. }
  543. if minDate.isNil && maxDate.isNil{
  544. return row
  545. }
  546. let adjustSeconds = row % 60
  547. if style.dateType == .HHmm || style.dateType == .HHmmss{
  548. if minDate?.hour ?? 0 == (maxDate?.hour ?? 0) && minDate?.minute ?? 0 == (maxDate?.minute ?? 0){
  549. //时 分 相同
  550. if adjustSeconds < (minDate?.second ?? 0){
  551. return fullMinuteOfSeconds + (minDate?.second ?? 0)
  552. }
  553. if adjustSeconds > (maxDate?.second ?? 0){
  554. return fullMinuteOfSeconds + (maxDate?.second ?? 0)
  555. }
  556. return row
  557. }
  558. if hour == (minDate?.hour ?? 0) && minute == (minDate?.minute ?? 0) && adjustSeconds < (minDate?.second ?? 0){
  559. return fullMinuteOfSeconds + (minDate?.second ?? 0)
  560. }
  561. if hour == (maxDate?.hour ?? 0) && minute == (maxDate?.minute ?? 0) && adjustSeconds > (maxDate?.second ?? 0){
  562. return fullMinuteOfSeconds + (maxDate?.second ?? 0)
  563. }
  564. return row
  565. }
  566. guard let year = year, let month = month, let day = day, let hour = hour, let minute = minute else{
  567. return row
  568. }
  569. //快速判断
  570. if minDate.isNotNil && maxDate.isNotNil{
  571. if adjustSeconds >= (minDate?.second ?? 0) && adjustSeconds <= (maxDate?.second ?? 0){
  572. return row
  573. }
  574. }
  575. else{
  576. if minDate.isNotNil && adjustSeconds >= (minDate?.second ?? 0){
  577. return row
  578. }
  579. if maxDate.isNotNil && adjustSeconds <= (maxDate?.second ?? 0){
  580. return row
  581. }
  582. }
  583. if minDate?.year ?? 0 == (maxDate?.year ?? 0) && minDate?.month ?? 0 == (maxDate?.month ?? 0) && minDate?.day ?? 0 == (maxDate?.day ?? 0) && minDate?.hour ?? 0 == (maxDate?.hour ?? 0) && minDate?.minute ?? 0 == (maxDate?.minute ?? 0){
  584. //年 月 日 时 分 相同
  585. if adjustSeconds < (minDate?.second ?? 0){
  586. return fullMinuteOfSeconds + (minDate?.second ?? 0)
  587. }
  588. if adjustSeconds > (maxDate?.second ?? 0){
  589. return fullMinuteOfSeconds + (maxDate?.second ?? 0)
  590. }
  591. return row
  592. }
  593. if year == (minDate?.year ?? 0) && month == (minDate?.month ?? 0) && day == (minDate?.day ?? 0) && hour == (minDate?.hour ?? 0) && minute == (minDate?.minute ?? 0) && adjustSeconds < (minDate?.second ?? 0){
  594. return fullMinuteOfSeconds + (minDate?.second ?? 0)
  595. }
  596. if year == (maxDate?.year ?? 0) && month == (maxDate?.month ?? 0) && day == (maxDate?.day ?? 0) && hour == (maxDate?.hour ?? 0) && minute == (maxDate?.minute ?? 0) && adjustSeconds > (maxDate?.second ?? 0){
  597. return fullMinuteOfSeconds + (maxDate?.second ?? 0)
  598. }
  599. return row
  600. }
  601. }
  602. extension MCDatePikerDelegate{
  603. //设置pikerView 旋转到最适合的月份
  604. func rotatingToAdjustMonth(_ pickerView: UIPickerView, component: Int = 1){
  605. pickerView.reloadComponent(component)
  606. //处理月份
  607. //判断当前选中的月份是否被限制
  608. let componentMonthRow = pickerView.selectedRow(inComponent: component)
  609. let componentAdjustMonthRow = self.getAdjustMonth(row: componentMonthRow)
  610. if componentMonthRow != componentAdjustMonthRow{
  611. pickerView.selectRow(componentAdjustMonthRow, inComponent: component, animated: true)
  612. month = componentAdjustMonthRow % 12 + 1
  613. }
  614. let date = Date(String.init(format: "%04d-%02d-01", (year ?? 1),(month ?? 1)))!
  615. monthOfDays = date.monthDays
  616. }
  617. //设置pikerview 旋转到最适合的天数
  618. func rotatingToAdjustDay(_ pickerView: UIPickerView, component: Int = 2){
  619. pickerView.reloadComponent(component)
  620. let componentDayRow = pickerView.selectedRow(inComponent: component)
  621. let componentAdjustDayRow = self.getAdjusetDay(row: componentDayRow, beforeDay: day)
  622. if componentAdjustDayRow != componentDayRow{
  623. //找到componentDayRow 对应的日期,先切换 componentAdjustDayRow 对应到相同的日期
  624. var fullMonthOfDays : Int
  625. if (componentAdjustDayRow + 1) % monthOfDays == 0{
  626. fullMonthOfDays = ((componentAdjustDayRow + 1) / monthOfDays - 1) * monthOfDays
  627. }
  628. else{
  629. fullMonthOfDays = (componentAdjustDayRow + 1) / monthOfDays * monthOfDays
  630. }
  631. pickerView.selectRow(fullMonthOfDays + (day ?? 0) - 1 , inComponent: component, animated: false)
  632. if day != (componentAdjustDayRow % monthOfDays + 1){
  633. pickerView.selectRow(componentAdjustDayRow, inComponent: component, animated: true)
  634. }
  635. day = componentAdjustDayRow % monthOfDays + 1
  636. }
  637. }
  638. /// 设置pickerView 旋转到最适合的小时
  639. /// - Parameters: - pickerView: <#pickerView description#>
  640. func rotatingToAdjustHour(_ pickerView: UIPickerView, component: Int = 3){
  641. pickerView.reloadComponent(component)
  642. //处理小时
  643. //判断当前选中的小时是否被限制
  644. let componentHourRow = pickerView.selectedRow(inComponent: component)
  645. let componentAdjustHourRow = self.getAdjustHour(row: componentHourRow)
  646. if componentHourRow != componentAdjustHourRow{
  647. pickerView.selectRow(componentAdjustHourRow, inComponent: component, animated: true)
  648. hour = componentAdjustHourRow % 24
  649. }
  650. }
  651. /// 设置pickerView 旋转到最适合的分钟
  652. /// - Parameters: pickerView: <#pickerView description#>
  653. func rotatingToAdjustMinute(_ pickerView: UIPickerView, component: Int = 4){
  654. pickerView.reloadComponent(component)
  655. //处理分钟
  656. //判断当前选中的分钟是否被限制
  657. let componentMinuteRow = pickerView.selectedRow(inComponent: component)
  658. let componentAdjustMinuteRow = self.getAdjustMinute(row: componentMinuteRow)
  659. if componentMinuteRow != componentAdjustMinuteRow{
  660. pickerView.selectRow(componentAdjustMinuteRow, inComponent: component, animated: true)
  661. minute = componentAdjustMinuteRow % 60
  662. }
  663. }
  664. /// 设置pickerView 旋转到最适合的秒
  665. /// - Parameter pickerView: <#pickerView description#>
  666. func rotatingToAdjustSeconds(_ pickerView: UIPickerView, component: Int = 5){
  667. pickerView.reloadComponent(component)
  668. //处理秒
  669. //判断当前选中的秒是否被限制
  670. let componentSecondRow = pickerView.selectedRow(inComponent: component)
  671. let componentAdjustSecondRow = self.getAdjustSeconds(row: componentSecondRow)
  672. if componentSecondRow != componentAdjustSecondRow{
  673. pickerView.selectRow(componentAdjustSecondRow, inComponent: component, animated: true)
  674. seconds = componentAdjustSecondRow % 60
  675. }
  676. }
  677. }