如何设置UIDatePickerModeCountDownTimer的最长时间?

时间:2015-06-24 09:52:35

标签: ios swift uidatepicker

我想问一下如何设置iOS Count Down Timer的最长时间? (例如,最多1小时30分钟)

倒数计时器从UIDatePicker的模式开始:

UIDatePicker

谢谢!

编辑:

有人说我必须设置最小/最大日期,我只是在故事板中设置它们但我没有看到任何区别:

(设置的时间是我当地时间+ - 30分钟)

Max/Min Time set

编辑:

来自Apple:

在倒数计时器模式(UIDatePickerModeCountDownTimer)中也会忽略最小和最大日期。

他们无论如何要做到这一点?

2 个答案:

答案 0 :(得分:4)

在UIDatePickerModeCountDownTimer的情况下,您可以通过编程方式处理它

添加在UIDatePicker的值发生变化时调用的事件。

<强>目标C

[self.datePicker addTarget:self action:@selector(datePickedValueChanged:)
     forControlEvents:UIControlEventValueChanged];

<强>夫特

self.datePicker.addTarget(self, action: Selector("datePickedValueChanged:"), forControlEvents: UIControlEvents.ValueChanged)

然后,如果所选值超出允许选择范围,您可以将DatePicker设置为允许的最大值(或任何您想要的值)

<强>目标C

- (void)datePickedValueChanged:(id)sender{
    if (self.datePicker.countDownDuration > 5400.0f) { //5400 seconds = 1h30min
        [self.datePicker setCountDownDuration: 60.0f]; //Defaults to 1 minute
    }
}

<强>夫特

func datePickedValueChanged (sender: UIDatePicker) {
    if (self.datePicker.countDownDuration > 5400) { //5400 seconds = 1h30min
        self.datePicker.countDownDuration = 60.0; //Defaults to 1 minute
    }
}

- 上一个回答:

我在Date或DateAndTime模式下使用UIDatePicker为其他人留下以前的答案,如果这可以帮助某些人

您可以设置UIDatePicker的最短和最长日期。

此处用户无法在当前时间之前选择时间,只需前进1小时30分钟。

任何选择其他时间的尝试都会使UIDatePicker自动返回到允许的时间间隔。

NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDate *currentDate = [NSDate date];

NSDateComponents *dateDelta = [[NSDateComponents alloc] init];
[dateDelta setDay:0];
[dateDelta setHour:1];
[dateDelta setMinute:30];
NSDate *maximumDate = [calendar dateByAddingComponents:dateDelta toDate:currentDate options:0];
[self.datePicker setMaximumDate:maximumDate];

[dateDelta setDay:0];
[dateDelta setHour:0];
[dateDelta setMinute:0];
NSDate *minimumDate = [calendar dateByAddingComponents:dateDelta toDate:currentDate options:0];

[self.datePicker setMinimumDate:minimumDate];

答案 1 :(得分:1)

您可以使用NSTimer并设置间隔,然后更新NSDatePicker

  1. 宣布了以前的全局变量int limiter = 0;NSDate持有人;

  2. 将目标添加到NSDatePicker

  3. [self.YourDatePicker addTarget:self action:@selector(dateChanged:)forControlEvents:UIControlEventValueChanged];
    

    将间隔设置为60 == 1min

    NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:60 target:self selector:@selector(updateTime) userInfo:nil repeats:YES];
    
    - (void) dateChanged:(UIDatePicker *)sender
    {
        // handle date changes
    
         NSDate *set = sender.date; // time setted 
    
        // compute the difference base from this to `[NSDate timeIntervalSinceDate:]`
    
        NSDate *now = [NSDate date];
    
        // logic `limit = now - set (greater then the current time)`
    }
    
    
    - (void)updateTime
    {
        NSLog(@"fired");
    
        limiter--; // 
    
        if (limiter == 0)
        {
            // TIME IS UP
            [timer invalidate];
            return;
        }
    
        NSDate *now = self.fourthProfileBirthdate.date;
    
        NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    
        NSDateComponents *offsetComponents = [[NSDateComponents alloc] init];
        [offsetComponents setYear:-1]; // countdown subtracting  here from the current time
    
        // NSDate
        self.YourDatePicker.date = [gregorian dateByAddingComponents:offsetComponents toDate:now options:0];
    
    }
    

    抱歉,我无法继续详细说明我需要回家..

    但希望这可以帮助你......干杯!