123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816 |
- //
- // MCDatePikerDelegate.swift
- // HCQuanfangtong
- //
- // Created by Apple on 2022/5/10.
- // Copyright © 2022 Jyp. All rights reserved.
- //
- import Foundation
- import SwiftDate
- protocol MCDatePikerDelegate : UIPickerViewDelegate, UIPickerViewDataSource{
-
- /// 样式
- var style : MCDatePikerView! {get set}
-
- /// 最小值
- var minDate : Date? {get set}
-
- /// 最大值
- var maxDate : Date? {get set}
-
- /// 当前日期
- var defaultDate : Date! {get set}
-
- /// 当前选择的月份有多少天
- var monthOfDays : Int! {get set}
-
- //MARK: - 被选中的年月日时分秒
- var year : Int? {get set}
- var month : Int? {get set}
- var day : Int? {get set}
- var hour : Int? {get set}
- var minute : Int? {get set}
- var seconds : Int? {get set}
-
-
-
- }
- extension MCDatePikerDelegate{
-
- /// 最终的日期
- var finallyDate : Date{
- get{
- Date.init(year: year ?? 1, month: month ?? 1, day: day ?? 1, hour: hour ?? 0, minute: minute ?? 0, second: seconds ?? 0)
- }
- }
-
- /// 可以选择的样式
- var pikerRowStyle : [NSAttributedString.Key : Any]{
- get{
- [NSAttributedString.Key.foregroundColor: self.style.delegateForPikerTitleColor(), NSAttributedString.Key.font: self.style.delegateForPikerTitleFont()]
- }
- }
-
- /// 不可以选择的样式
- var pikerDisableStyle : [NSAttributedString.Key : Any]{
- get{
- [NSAttributedString.Key.foregroundColor: self.style.delegateForPikerTitleDisableColor(), NSAttributedString.Key.font: self.style.delegateForPikerTitleDisableFont()]
- }
- }
-
-
- /// 当前年份是否可以被选中
- /// - Parameter row: <#row description#>
- /// - Returns: <#description#>
- func canBeSelectedOfYear(row: Int) -> Bool{
- if (minDate.isNotNil && row + 1 < minDate!.year) || (maxDate.isNotNil && row + 1 > maxDate!.year){
- return false
- }
- return true
- }
-
-
- /// 当前月份是否可以被选中
- /// - Parameter row: <#row description#>
- /// - Returns: <#description#>
- func canBeSelectedOfMonth(row: Int) -> Bool{
- if minDate.isNil && maxDate.isNil{
- return true
- }
- let adjustMonth = row % 12 + 1
- if minDate?.year ?? 0 == (maxDate?.year ?? 0){
- //年份相同
- if adjustMonth < (minDate?.month ?? 0){
- return false
- }
- if adjustMonth > (maxDate?.month ?? 0){
- return false
- }
- }
- else{
- //年分不同
- if year == (minDate?.year ?? 0) && adjustMonth < (minDate?.month ?? 0){
- return false
- }
- if year == (maxDate?.year ?? 0) && adjustMonth > (maxDate?.month ?? 0){
- return false
- }
- }
- return true
- }
-
- /// 当前日期是否可以被选中
- /// - Parameter row: <#row description#>
- /// - Returns: <#description#>
- func canBeSelectedOfDay(row: Int) -> Bool{
- if minDate.isNil && maxDate.isNil{
- return true
- }
- //当前月份有多少天
- let daysOfMonth : Int = monthOfDays
- //当前row 对应的月份
- let adjustDay = row % daysOfMonth + 1
-
- if minDate?.year ?? 0 == (maxDate?.year ?? 0) && minDate?.month ?? 0 == (maxDate?.month ?? 0){
- //年相同 月相同
- if adjustDay < (minDate?.day ?? 0){
- return false
- }
- if adjustDay > (maxDate?.day ?? 0){
- return false
- }
- return true
- }
- if year == (minDate?.year ?? 0) && month == (minDate?.month ?? 0) && adjustDay < (minDate?.day ?? 0){
- return false
- }
- if year == (maxDate?.year ?? 0) && month == (maxDate?.month ?? 0) && adjustDay > (maxDate?.day ?? 0){
- return false
- }
- return true
- }
-
- /// 当前小时 是否可以被选中
- /// - Parameter row: <#row description#>
- /// - Returns: <#description#>
- func canBeSelectedOfHour(row: Int) -> Bool{
-
- if minDate.isNil && maxDate.isNil{
- return true
- }
-
- let adjustHour = row % 24
-
- if style.dateType == .HHmm || style.dateType == .HHmmss{
- if minDate.isNotNil && adjustHour < (minDate?.hour ?? 0){
- return false
- }
- if maxDate.isNotNil && adjustHour > (maxDate?.hour ?? 0){
- return false
- }
- return true
- }
-
- //快速判断
- if minDate.isNotNil && maxDate.isNotNil{
- if adjustHour >= (minDate?.hour ?? 0) && adjustHour <= (maxDate?.hour ?? 0){
- return true
- }
- }
- else{
- if minDate.isNotNil && adjustHour >= (minDate?.hour ?? 0){
- return true
- }
- if maxDate.isNotNil && adjustHour <= (maxDate?.hour ?? 0){
- return true
- }
- }
-
- //其他情况
- if minDate?.year ?? 0 == (maxDate?.year ?? 0) && minDate?.month ?? 0 == (maxDate?.month ?? 0) && minDate?.day ?? 0 == (maxDate?.day ?? 0){
- //年相同 月相同 日相同
- if adjustHour < (minDate?.hour ?? 0){
- return false
- }
- if adjustHour > (maxDate?.hour ?? 0){
- return false
- }
- return true
-
- }
- if year == (minDate?.year ?? 0) && month == (minDate?.month ?? 0) && day == (minDate?.day ?? 0) && adjustHour < (minDate?.hour ?? 0){
- return false
- }
- if year == (maxDate?.year ?? 0) && month == (maxDate?.month ?? 0) && day == (maxDate?.day ?? 0) && adjustHour > (maxDate?.hour ?? 0){
- return false
- }
- return true
-
- }
-
- /// 当前分钟 是否可以被选中
- /// - Parameter row: <#row description#>
- /// - Returns: <#description#>
- func canBeSelectedOfMinute(row: Int) -> Bool{
-
- if minDate.isNil && maxDate.isNil{
- return true
- }
-
- let adjustMinute = row % 60
-
- if style.dateType == .HHmm || style.dateType == .HHmmss{
-
- if minDate?.hour ?? 0 == (maxDate?.hour ?? 0){
- //小时相同
- if adjustMinute < (minDate?.minute ?? 0){
- return false
- }
- if adjustMinute > (maxDate?.minute ?? 0){
- return false
- }
- }
- else{
- //小时不同
- if hour == (minDate?.hour ?? 0) && adjustMinute < (minDate?.minute ?? 0){
- return false
- }
- if hour == (maxDate?.hour ?? 0) && adjustMinute > (maxDate?.minute ?? 0){
- return false
- }
- }
- return true
- }
-
-
- //快速判断
- if minDate.isNotNil && maxDate.isNotNil{
- if adjustMinute >= (minDate?.minute ?? 0) && adjustMinute <= (maxDate?.minute ?? 0){
- return true
- }
- }
- else{
- if minDate.isNotNil && adjustMinute >= (minDate?.minute ?? 0){
- return true
- }
- if maxDate.isNotNil && adjustMinute <= (maxDate?.minute ?? 0){
- return true
- }
- }
-
- //其他情况
- 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){
- //年 月 日 时 相同
- if adjustMinute < (minDate?.minute ?? 0){
- return false
- }
- if adjustMinute > (maxDate?.minute ?? 0){
- return false
- }
- return true
- }
- if year == (minDate?.year ?? 0) && month == (minDate?.month ?? 0) && day == (minDate?.day ?? 0) && hour == (minDate?.hour ?? 0) && adjustMinute < (minDate?.minute ?? 0){
- return false
- }
- if year == (maxDate?.year ?? 0) && month == (maxDate?.month ?? 0) && day == (maxDate?.day ?? 0) && hour == (maxDate?.hour ?? 0) && adjustMinute > (maxDate?.minute ?? 0){
- return false
- }
- return true
-
- }
-
- /// 当前秒 是否可以被选中
- /// - Parameter row: <#row description#>
- /// - Returns: <#description#>
- func canBeSelectedOfSecond(row: Int) -> Bool{
-
- if minDate.isNil && maxDate.isNil{
- return true
- }
-
- let adjustSeconds = row % 60
-
- if style.dateType == .HHmm || style.dateType == .HHmmss{
-
- if minDate?.hour ?? 0 == (maxDate?.hour ?? 0) && minDate?.minute ?? 0 == (maxDate?.minute ?? 0){
- //时 分 相同
- if adjustSeconds < (minDate?.second ?? 0){
- return false
- }
- if adjustSeconds > (maxDate?.second ?? 0){
- return false
- }
- return true
-
- }
- if hour == (minDate?.hour ?? 0) && minute == (minDate?.minute ?? 0) && adjustSeconds < (minDate?.second ?? 0){
- return false
- }
- if hour == (maxDate?.hour ?? 0) && minute == (maxDate?.minute ?? 0) && adjustSeconds > (maxDate?.second ?? 0){
- return false
- }
- return true
- }
-
-
- //快速判断
- if minDate.isNotNil && maxDate.isNotNil{
- if adjustSeconds >= (minDate?.second ?? 0) && adjustSeconds <= (maxDate?.second ?? 0){
- return true
- }
- }
- else{
- if minDate.isNotNil && adjustSeconds >= (minDate?.second ?? 0){
- return true
- }
- if maxDate.isNotNil && adjustSeconds <= (maxDate?.second ?? 0){
- return true
- }
- }
-
- //其他情况
- 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){
- //年 月 日 时 分 相同
- if adjustSeconds < (minDate?.second ?? 0){
- return false
- }
- if adjustSeconds > (maxDate?.second ?? 0){
- return false
- }
- return true
- }
- 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){
- return false
- }
- 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){
- return false
- }
- return true
-
- }
-
-
- }
- extension MCDatePikerDelegate{
- /// 获取row 对应的最适合的月份
- /// - Parameters:
- /// - delegate: <#delegate description#>
- /// - row: 当前components 的row
- /// - targetMonth: 目标月份,想要调整到多少月
- /// - Returns: 返回最适合的row
- func getAdjustMonth(row: Int, targetMonth: Int? = nil) -> Int{
-
- guard let year = year else{
- return row
- }
-
- let adjustMonth = row % 12 + 1
- let fullYearOfMonths = (row + 1) / 12 * 12
-
- if targetMonth.isNotNil{
- return fullYearOfMonths + targetMonth! - 1
- }
- if minDate.isNil && maxDate.isNil{
- return row
- }
-
- if minDate?.year ?? 0 == (maxDate?.year ?? 0){
- //年份相同
- if adjustMonth < (minDate?.month ?? 0){
- return fullYearOfMonths + (minDate?.month ?? 0) - 1
- }
- if adjustMonth > (maxDate?.month ?? 0){
- return fullYearOfMonths + (maxDate?.month ?? 0) - 1
- }
- }
- else{
- //年分不同
- if year == (minDate?.year ?? 0) && adjustMonth < (minDate?.month ?? 0){
- return fullYearOfMonths + (minDate?.month ?? 0) - 1
- }
- if year == (maxDate?.year ?? 0) && adjustMonth > (maxDate?.month ?? 0){
- return fullYearOfMonths + (maxDate?.month ?? 0) - 1
- }
- }
- return row
-
-
- }
-
- /// 获取row 下对应的最适合的天数
- /// - Parameters:
- /// - delegate: <#delegate description#>
- /// - row: 当前components的row
- /// - beforeDay: 之前选中的天
- /// - targetDay: 目标天数
- /// - Returns: 返回最适合的天数
- func getAdjusetDay(row: Int, beforeDay: Int? = nil, targetDay: Int? = nil) -> Int{
-
- var adjustRow = self.getBeChangedDay(row: row, beforeDay: beforeDay, targetDay: targetDay)
- if beforeDay.isNil{
- return adjustRow
- }
- if adjustRow != row{
- //天数
- if adjustRow - row > monthOfDays{
- adjustRow = adjustRow - (adjustRow - row)/monthOfDays * monthOfDays
- }
- else if row - adjustRow < monthOfDays{
- adjustRow = adjustRow + (row - adjustRow)/monthOfDays * monthOfDays
- }
- adjustRow = row - (row % monthOfDays + 1 - (adjustRow % monthOfDays + 1))
- }
- return adjustRow
-
- }
-
- private func getBeChangedDay(row: Int, beforeDay: Int? = nil, targetDay: Int? = nil) -> Int{
-
-
- guard let year = year, let month = month else{
- return row
- }
-
-
- let daysOfMonth = monthOfDays!
-
- var fullMonthOfDays : Int
- if (row + 1) % daysOfMonth == 0{
- fullMonthOfDays = ((row + 1) / daysOfMonth - 1) * daysOfMonth
- }
- else{
- fullMonthOfDays = (row + 1) / daysOfMonth * daysOfMonth
- }
-
-
- if targetDay.isNotNil{
- //算当前year 和 month 下 天数
- if targetDay! > daysOfMonth{
- fatalError("数据有问题")
- }
- return fullMonthOfDays + targetDay! - 1
- }
-
- if minDate.isNil && maxDate.isNil{
- if beforeDay.isNil{
- return row
- }
- if beforeDay! <= daysOfMonth{
- return fullMonthOfDays + beforeDay! - 1
- }
- return fullMonthOfDays + daysOfMonth - 1
-
- }
- var adjustDay = 0
- if beforeDay.isNotNil{
- adjustDay = (beforeDay! > daysOfMonth ? daysOfMonth : beforeDay!)
- }
- else{
- adjustDay = row % daysOfMonth + 1
- }
-
- if minDate?.year ?? 0 == (maxDate?.year ?? 0) && minDate?.month ?? 0 == (maxDate?.month ?? 0){
- //年 月 相同
- if adjustDay < (minDate?.day ?? 0){
- return fullMonthOfDays + minDate!.day - 1
- }
- if adjustDay > (maxDate?.day ?? 0){
- return fullMonthOfDays + maxDate!.day - 1
- }
- return fullMonthOfDays + adjustDay - 1
- }
- if year == (minDate?.year ?? 0) && month == (minDate?.month ?? 0) && adjustDay < (minDate?.day ?? 0){
- return fullMonthOfDays + minDate!.day - 1
- }
- if year == (maxDate?.year ?? 0) && month == (maxDate?.month ?? 0) && adjustDay > (maxDate?.day ?? 0){
- return fullMonthOfDays + maxDate!.day - 1
- }
- return fullMonthOfDays + adjustDay - 1
-
-
- }
-
-
- /// 获取row 对应的最适合的小时
- /// - Parameters:
- /// - delegate: <#delegate description#>
- /// - row: 当前components 的row
- /// - targetHour: 目标月份,想要调整到多少月
- /// - Returns: 返回最适合的row
- func getAdjustHour(row: Int, targetHour: Int? = nil) -> Int{
-
- let fullDayOfHours = row / 24 * 24
-
- if targetHour.isNotNil{
- return fullDayOfHours + targetHour!
- }
-
- if minDate.isNil && maxDate.isNil{
- return row
- }
-
- let adjustHour = row % 24
-
- if style.dateType == .HHmm || style.dateType == .HHmmss{
- if minDate.isNotNil && adjustHour < (minDate?.hour ?? 0){
- return fullDayOfHours + (minDate?.hour ?? 0)
- }
- if maxDate.isNotNil && adjustHour > (maxDate?.hour ?? 0){
- return fullDayOfHours + (minDate?.hour ?? 0)
- }
- return row
- }
-
- guard let year = year, let month = month, let day = day else{
- return row
- }
-
-
- //快速判断
- if minDate.isNotNil && maxDate.isNotNil{
- if adjustHour >= (minDate?.hour ?? 0) && adjustHour <= (maxDate?.hour ?? 0){
- return row
- }
- }
- else{
- if minDate.isNotNil && adjustHour >= (minDate?.hour ?? 0){
- return row
- }
- if maxDate.isNotNil && adjustHour <= (maxDate?.hour ?? 0){
- return row
- }
- }
-
-
- //其他情况
- if minDate?.year ?? 0 == (maxDate?.year ?? 0) && minDate?.month ?? 0 == (maxDate?.month ?? 0) && minDate?.day ?? 0 == (maxDate?.day ?? 0){
- //年相同 月相同 日相同
- if adjustHour < (minDate?.hour ?? 0){
- return fullDayOfHours + (minDate?.hour ?? 0)
- }
- if adjustHour > (maxDate?.hour ?? 0){
- return fullDayOfHours + (maxDate?.hour ?? 0)
- }
- return row
-
- }
- if year == (minDate?.year ?? 0) && month == (minDate?.month ?? 0) && day == (minDate?.day ?? 0) && adjustHour < (minDate?.hour ?? 0){
- return fullDayOfHours + (minDate?.hour ?? 0)
- }
- if year == (maxDate?.year ?? 0) && month == (maxDate?.month ?? 0) && day == (maxDate?.day ?? 0) && adjustHour > (maxDate?.hour ?? 0){
- return fullDayOfHours + (maxDate?.hour ?? 0)
- }
- return row
-
-
- }
-
- /// 获取row 对应的最适合的分钟
- /// - Parameters:
- /// - row: <#row description#>
- /// - targetMinute: <#targetMinute description#>
- /// - Returns: <#description#>
- func getAdjustMinute(row: Int, targetMinute: Int? = nil) -> Int{
-
- let fullHourOfMinutes = row / 60 * 60
-
- if targetMinute.isNotNil{
- return fullHourOfMinutes + targetMinute!
- }
-
- if minDate.isNil && maxDate.isNil{
- return row
- }
-
- let adjustMinute = row % 60
-
- if style.dateType == .HHmm || style.dateType == .HHmmss{
-
- if minDate?.hour ?? 0 == (maxDate?.hour ?? 0){
- //小时相同
- if adjustMinute < (minDate?.minute ?? 0){
- return fullHourOfMinutes + (minDate?.minute ?? 0)
- }
- if adjustMinute > (maxDate?.minute ?? 0){
- return fullHourOfMinutes + (maxDate?.minute ?? 0)
- }
- }
- else{
- //小时不同
- if hour == (minDate?.hour ?? 0) && adjustMinute < (minDate?.minute ?? 0){
- return fullHourOfMinutes + (minDate?.minute ?? 0)
- }
- if hour == (maxDate?.hour ?? 0) && adjustMinute > (maxDate?.minute ?? 0){
- return fullHourOfMinutes + (maxDate?.minute ?? 0)
- }
- }
- return row
- }
-
-
- guard let year = year, let month = month, let day = day, let hour = hour else{
- return row
- }
-
-
- //快速判断
- if minDate.isNotNil && maxDate.isNotNil{
- if adjustMinute >= (minDate?.minute ?? 0) && adjustMinute <= (maxDate?.minute ?? 0){
- return row
- }
- }
- else{
- if minDate.isNotNil && adjustMinute >= (minDate?.minute ?? 0){
- return row
- }
- if maxDate.isNotNil && adjustMinute <= (maxDate?.minute ?? 0){
- return row
- }
- }
-
- 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){
- //年 月 日 时 相同
- if adjustMinute < (minDate?.minute ?? 0){
- return fullHourOfMinutes + (minDate?.minute ?? 0)
- }
- if adjustMinute > (maxDate?.minute ?? 0){
- return fullHourOfMinutes + (maxDate?.minute ?? 0)
- }
- return row
- }
- if year == (minDate?.year ?? 0) && month == (minDate?.month ?? 0) && day == (minDate?.day ?? 0) && hour == (minDate?.hour ?? 0) && adjustMinute < (minDate?.minute ?? 0){
- return fullHourOfMinutes + (minDate?.minute ?? 0)
- }
- if year == (maxDate?.year ?? 0) && month == (maxDate?.month ?? 0) && day == (maxDate?.day ?? 0) && hour == (maxDate?.hour ?? 0) && adjustMinute > (maxDate?.minute ?? 0){
- return fullHourOfMinutes + (maxDate?.minute ?? 0)
- }
- return row
- }
-
-
- /// 获取row 对应的最适合的分钟
- /// - Parameters:
- /// - row: <#row description#>
- /// - targetSeconds: <#targetMinute description#>
- /// - Returns: <#description#>
- func getAdjustSeconds(row: Int, targetSeconds: Int? = nil) -> Int{
-
-
-
- let fullMinuteOfSeconds = row / 60 * 60
-
- if targetSeconds.isNotNil{
- return fullMinuteOfSeconds + targetSeconds!
- }
-
- if minDate.isNil && maxDate.isNil{
- return row
- }
-
- let adjustSeconds = row % 60
-
- if style.dateType == .HHmm || style.dateType == .HHmmss{
-
- if minDate?.hour ?? 0 == (maxDate?.hour ?? 0) && minDate?.minute ?? 0 == (maxDate?.minute ?? 0){
- //时 分 相同
- if adjustSeconds < (minDate?.second ?? 0){
- return fullMinuteOfSeconds + (minDate?.second ?? 0)
- }
- if adjustSeconds > (maxDate?.second ?? 0){
- return fullMinuteOfSeconds + (maxDate?.second ?? 0)
- }
- return row
-
- }
- if hour == (minDate?.hour ?? 0) && minute == (minDate?.minute ?? 0) && adjustSeconds < (minDate?.second ?? 0){
- return fullMinuteOfSeconds + (minDate?.second ?? 0)
- }
- if hour == (maxDate?.hour ?? 0) && minute == (maxDate?.minute ?? 0) && adjustSeconds > (maxDate?.second ?? 0){
- return fullMinuteOfSeconds + (maxDate?.second ?? 0)
- }
- return row
-
- }
-
-
- guard let year = year, let month = month, let day = day, let hour = hour, let minute = minute else{
- return row
- }
-
- //快速判断
- if minDate.isNotNil && maxDate.isNotNil{
- if adjustSeconds >= (minDate?.second ?? 0) && adjustSeconds <= (maxDate?.second ?? 0){
- return row
- }
- }
- else{
- if minDate.isNotNil && adjustSeconds >= (minDate?.second ?? 0){
- return row
- }
- if maxDate.isNotNil && adjustSeconds <= (maxDate?.second ?? 0){
- return row
- }
- }
-
- 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){
- //年 月 日 时 分 相同
- if adjustSeconds < (minDate?.second ?? 0){
- return fullMinuteOfSeconds + (minDate?.second ?? 0)
- }
- if adjustSeconds > (maxDate?.second ?? 0){
- return fullMinuteOfSeconds + (maxDate?.second ?? 0)
- }
- return row
- }
- 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){
- return fullMinuteOfSeconds + (minDate?.second ?? 0)
- }
- 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){
- return fullMinuteOfSeconds + (maxDate?.second ?? 0)
- }
- return row
-
- }
-
- }
- extension MCDatePikerDelegate{
-
- //设置pikerView 旋转到最适合的月份
- func rotatingToAdjustMonth(_ pickerView: UIPickerView, component: Int = 1){
-
- pickerView.reloadComponent(component)
- //处理月份
- //判断当前选中的月份是否被限制
- let componentMonthRow = pickerView.selectedRow(inComponent: component)
- let componentAdjustMonthRow = self.getAdjustMonth(row: componentMonthRow)
- if componentMonthRow != componentAdjustMonthRow{
- pickerView.selectRow(componentAdjustMonthRow, inComponent: component, animated: true)
- month = componentAdjustMonthRow % 12 + 1
- }
-
- let date = Date(String.init(format: "%04d-%02d-01", (year ?? 1),(month ?? 1)))!
- monthOfDays = date.monthDays
-
- }
-
- //设置pikerview 旋转到最适合的天数
- func rotatingToAdjustDay(_ pickerView: UIPickerView, component: Int = 2){
-
- pickerView.reloadComponent(component)
- let componentDayRow = pickerView.selectedRow(inComponent: component)
- let componentAdjustDayRow = self.getAdjusetDay(row: componentDayRow, beforeDay: day)
- if componentAdjustDayRow != componentDayRow{
- //找到componentDayRow 对应的日期,先切换 componentAdjustDayRow 对应到相同的日期
-
- var fullMonthOfDays : Int
- if (componentAdjustDayRow + 1) % monthOfDays == 0{
- fullMonthOfDays = ((componentAdjustDayRow + 1) / monthOfDays - 1) * monthOfDays
- }
- else{
- fullMonthOfDays = (componentAdjustDayRow + 1) / monthOfDays * monthOfDays
- }
-
- pickerView.selectRow(fullMonthOfDays + (day ?? 0) - 1 , inComponent: component, animated: false)
- if day != (componentAdjustDayRow % monthOfDays + 1){
- pickerView.selectRow(componentAdjustDayRow, inComponent: component, animated: true)
- }
- day = componentAdjustDayRow % monthOfDays + 1
- }
-
- }
-
- /// 设置pickerView 旋转到最适合的小时
- /// - Parameters: - pickerView: <#pickerView description#>
- func rotatingToAdjustHour(_ pickerView: UIPickerView, component: Int = 3){
-
- pickerView.reloadComponent(component)
- //处理小时
- //判断当前选中的小时是否被限制
- let componentHourRow = pickerView.selectedRow(inComponent: component)
- let componentAdjustHourRow = self.getAdjustHour(row: componentHourRow)
- if componentHourRow != componentAdjustHourRow{
- pickerView.selectRow(componentAdjustHourRow, inComponent: component, animated: true)
- hour = componentAdjustHourRow % 24
- }
- }
-
- /// 设置pickerView 旋转到最适合的分钟
- /// - Parameters: pickerView: <#pickerView description#>
- func rotatingToAdjustMinute(_ pickerView: UIPickerView, component: Int = 4){
-
- pickerView.reloadComponent(component)
- //处理分钟
- //判断当前选中的分钟是否被限制
- let componentMinuteRow = pickerView.selectedRow(inComponent: component)
- let componentAdjustMinuteRow = self.getAdjustMinute(row: componentMinuteRow)
- if componentMinuteRow != componentAdjustMinuteRow{
- pickerView.selectRow(componentAdjustMinuteRow, inComponent: component, animated: true)
- minute = componentAdjustMinuteRow % 60
- }
- }
-
-
- /// 设置pickerView 旋转到最适合的秒
- /// - Parameter pickerView: <#pickerView description#>
- func rotatingToAdjustSeconds(_ pickerView: UIPickerView, component: Int = 5){
-
- pickerView.reloadComponent(component)
- //处理秒
- //判断当前选中的秒是否被限制
- let componentSecondRow = pickerView.selectedRow(inComponent: component)
- let componentAdjustSecondRow = self.getAdjustSeconds(row: componentSecondRow)
- if componentSecondRow != componentAdjustSecondRow{
- pickerView.selectRow(componentAdjustSecondRow, inComponent: component, animated: true)
- seconds = componentAdjustSecondRow % 60
- }
- }
-
-
- }
|