// // 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 } } }