UIDatePicker odd behavior when setting minuteInterval
2024-09-06 04:02:47
http://stackoverflow.com/questions/6948297/uidatepicker-odd-behavior-when-setting-minuteinterval
Here's yet another approach, with an Objective-C category!
I took the spirit of @zurbergram's rounding behavior (up/down to closest) and @mmorris's overall answer and came up with this category:
#import <UIKit/UIKit.h>
@interface UIDatePicker (SetDateRounded)
-(void)setMinimumDateRoundedByMinuteInterval:(NSDate *)minimumDate;
-(void)setDateRoundedByMinuteInterval:(NSDate *)date animated:(BOOL)animatedYesNo;
@end
@implementation UIDatePicker (SetDateRounded)
-(void)setDateRoundedByMinuteInterval:(NSDate *)date animated:(BOOL)animatedYesNo
{
NSDateComponents *dateComponents = [[NSCalendar currentCalendar] components:NSMinuteCalendarUnit fromDate:date];
NSInteger minutes = [dateComponents minute];
NSInteger minutesRounded = roundf((float)minutes / (float)[self minuteInterval]) * self.minuteInterval;
NSDate *roundedDate = [[NSDate alloc] initWithTimeInterval:60.0 * (minutesRounded - minutes) sinceDate:date];
[self setDate:roundedDate animated:animatedYesNo];
}
-(void)setMinimumDateRoundedByMinuteInterval:(NSDate *)date
{
NSDateComponents *dateComponents = [[NSCalendar currentCalendar] components:NSMinuteCalendarUnit fromDate:date];
NSInteger minutes = [dateComponents minute];
NSInteger minutesRounded = roundf((float)minutes / (float)[self minuteInterval]) * self.minuteInterval;
NSDate *roundedDate = [[NSDate alloc] initWithTimeInterval:60.0 * (minutesRounded - minutes) sinceDate:date];
[self setMinimumDate:roundedDate];
}
@end
Then in your implementation, you can do something like this:
#import "UIDatePicker+SetDateRounded.h"
...
- (void)viewDidLoad
{
[super viewDidLoad];
_datePicker.minuteInterval = 15;
[_datePicker setMinimumDateRoundedByMinuteInterval:[NSDate date]];
[_datePicker setDateRoundedByMinuteInterval:[NSDate date] animated:YES];
}
版权声明:本文为博主原创文章,未经博主允许不得转载。
最新文章
- TCP四种计时器
- 如何设置让基于matplotlib的绘图库正常的显示no-ascii字符(中文字符)
- SOA和WCF&;WebAPI
- 【转载】HTTP 错误 500.19 - Internal Server Error
- Android 项目实战--手机卫士(实现splash)
- mysql 乱码问题
- js模拟类
- AIX修改用户密码登录不成功案例分享
- ACM题目————已知前序和中序求后序
- OpenGL第15,16,17讲小结
- 2016 GDCPC 省赛总结
- quick-cocos2d-x游戏开发【5】——创建菜单
- 【卷一】正则四 |>; 练习
- PKI 笔记
- Linux之shell编程函数使用
- 蓝桥杯-循环节长度-java
- SVG 入门——理解viewport,viewbox,preserveAspectRatio
- 转发 JBPM工作流小结
- C# 文字滚动特效(上下滚动)
- vs2012升级vs2017后的一些坑