mrgrieves
mrgrieves

Reputation: 579

iOS 6 UIDatePicker timer lets user select 0 countDownDuration

I'm using a UIDatePicker in UIDatePickerModeCountDownTimer to allow the user to select a duration.

If you set the timer interval to 5 minutes, the user will be able to select 0 hours : 0 minutes (which is bad; this is not allowed in iOS 5 or even in iOS 6 with a 1 minute interval).

Right now I'm fixing it by doing this on change:

-(void)timerValueChanged
{
    int clockInterval = workoutTimePicker.minuteInterval * 60;

    if (workoutTimePicker.countDownDuration < clockInterval) {
        workoutTimePicker.countDownDuration = clockInterval;
    }
}

But that makes the roller jump. How can I animate the roller?

Upvotes: 1

Views: 1493

Answers (2)

Marco
Marco

Reputation: 1686

While trying out Carlos Ricardo's answer, I've found out a more concise solution (using swift):

let minDate = Calendar.current.startOfDay(for: Date()) + clockInterval
datePicker.setDate(minDate, animated: true)

Upvotes: 0

Carlos Ricardo
Carlos Ricardo

Reputation: 2086

There is currently no setCountDownDuration:animated: so we have to use setDate:animated:.

We must create a NSDate object with current day/month/year to allow us to fill the minutes component, with your desired value, let's say it's workoutTimePicker.minuteInterval:

NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *components = [calendar components:NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit fromDate:[NSDate date]];
    components.hour = 0;
    components.minute = workoutTimePicker.minuteInterval; // how much minutes you want
    components.second = 0;
NSDate * resetedMinsDate = [calendar dateFromComponents:components];        

So with that new date, we just need to set the picker's date:

[workoutTimePicker setDate:resetedMinsDate animated:YES];

Upvotes: 2

Related Questions