zoukankan      html  css  js  c++  java
  • UIDatePicker odd behavior when setting minuteInterval

    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];
    }

    版权声明:本文为博主原创文章,未经博主允许不得转载。

  • 相关阅读:
    wcf布到服务器下载文件大小限制问题
    动态修改母版页中的DIV标签中的LI的A的CLASS属性
    c# 分页的方法
    Java学习笔记-异常处理-有风险的行为
    ASP.NET Core中的静态文件
    在ASP.NET Core中进行配置
    ASP.NET Core中的中间件和请求管道
    Kestrel:ASP.NET Core的Web服务器
    ASP.NET Core Program.cs
    ASP.Net Core Startup类
  • 原文地址:https://www.cnblogs.com/zsw-1993/p/4879480.html
Copyright © 2011-2022 走看看