xcode UIDatePicker 以 15m 为间隔,但总是精确的时间作为返回值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7504060/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 22:00:08  来源:igfitidea点击:

UIDatePicker with 15m interval but always exact time as return value

objective-ciosxcodeuidatepicker

提问by TigreFurry

I've got a UIDatePicker in Time mode with an interval of 15 minutes.

我在时间模式下有一个 UIDatePicker,间隔为 15 分钟。

Let's say it's 7:22PM. The value that the date picker shows per default is the current time rounded to the next higher/lower value, in this case, it's 7:15PM. If the user interacts with the date picker, the value that's shown is also the value returned. However if there hasn't been any user interaction and I get the time with [UIDatePicker date]the return value is the exact current time, i.e. 7:22 to stick with the example. Is there any way to always get the value that's actually shown on the date picker instead of the current unrounded time, even if the user hasn't explicitly picked a value? I've already tried to set the picker manually with [UIDatePicker setDate:[NSDate date]]but that doesn't change the behavior.

假设现在是晚上 7:22。日期选择器默认显示的值是四舍五入到下一个更高/更低值的当前时间,在这种情况下,它是晚上 7:15。如果用户与日期选择器交互,则显示的值也是返回的值。但是,如果没有任何用户交互并且我得到[UIDatePicker date]返回值的时间是确切的当前时间,即 7:22 坚持这个例子。有什么方法可以始终获取日期选择器上实际显示的值而不是当前未取整的时间,即使用户没有明确选择一个值?我已经尝试手动设置选择器,[UIDatePicker setDate:[NSDate date]]但这不会改变行为。

Any hints? Thanks in advance!

任何提示?提前致谢!

回答by aasatt

I've modified @ima747 's answer for Swift 3/4 and as an extension of UIDatePicker. picker.clampedDate

我已经修改了 @ima747 对 Swift 3/4 的回答,并作为UIDatePicker.picker.clampedDate

extension UIDatePicker {
    /// Returns the date that reflects the displayed date clamped to the `minuteInterval` of the picker.
    /// - note: Adapted from [ima747's](http://stackoverflow.com/users/463183/ima747) answer on [Stack Overflow](http://stackoverflow.com/questions/7504060/uidatepicker-with-15m-interval-but-always-exact-time-as-return-value/42263214#42263214})
    public var clampedDate: Date {
        let referenceTimeInterval = self.date.timeIntervalSinceReferenceDate
        let remainingSeconds = referenceTimeInterval.truncatingRemainder(dividingBy: TimeInterval(minuteInterval*60))
        let timeRoundedToInterval = referenceTimeInterval - remainingSeconds
        return Date(timeIntervalSinceReferenceDate: timeRoundedToInterval)
    }
}

回答by ima747

This isn't a perfect solution but it works for me. I've taken it from another post and modified it to accept different values.

这不是一个完美的解决方案,但它对我有用。我从另一篇文章中获取并修改了它以接受不同的值。

- (NSDate*)clampDate:(NSDate *)dt toMinutes:(int)minutes {
int referenceTimeInterval = (int)[dt timeIntervalSinceReferenceDate];
int remainingSeconds = referenceTimeInterval % (minutes*60);
int timeRoundedTo5Minutes = referenceTimeInterval - remainingSeconds; 
if(remainingSeconds>((minutes*60)/2)) {/// round up
    timeRoundedTo5Minutes = referenceTimeInterval +((minutes*60)-remainingSeconds);            
}
 return [NSDate dateWithTimeIntervalSinceReferenceDate:(NSTimeInterval)timeRoundedTo5Minutes];
}

Feed it an input date and a minute value (taken from the UIDatePicker.minuteInterval in the case of this thread) and it will return a time clamped to that interval which you can feed to the picker.

为其提供输入日期和分钟值(在此线程的情况下从 UIDatePicker.minuteInterval 中获取),它将返回一个固定到该间隔的时间,您可以将其提供给选择器。

回答by Bhavin Chauhan

@ima747 is right if I've changed date picker's default value then it's working fine and if I've read date from picker control without onChangethen it's returning wrong date value.
But I've checked that, date picker control have set default date (current date and may be it use nearest time milliseconds) If I set it to custom date and also set time to any date and 1:00:AM so now my time picker always open with 1:00:AM instead of current time enter image description here

@ima747 是正确的,如果我更改了日期选择器的默认值,那么它工作正常,如果我在没有onChange 的情况下从选择器控件中读取了日期,那么它返回了错误的日期值。
但是我已经检查过,日期选择器控件已设置默认日期(当前日期,可能是使用最近的时间毫秒)如果我将其设置为自定义日期并将时间设置为任何日期和 1:00:AM 所以现在我的时间选择器总是在 1:00:AM 而不是当前时间打开 在此处输入图片说明