Reputation: 2642
I want to ask that how can I set the iOS Count Down Timer's maximum time? (eg. 1 hour and 30 minutes at most)
Count Down Timer is get from UIDatePicker
's mode:
Thanks!
EDIT:
Someone said I have to set the minimum/maximum date, and I just set them in the storyboard but I don't see any difference:
(the time of the setting is my local time +- 30 minutes)
EDIT:
From Apple:
The minimum and maximum dates are also ignored in the countdown-timer mode (UIDatePickerModeCountDownTimer).
So is their anyway to do this?
Upvotes: 6
Views: 7695
Reputation: 3422
In your case for UIDatePickerModeCountDownTimer you can handle it programmatically
Add an event called when the value of your UIDatePicker
has changed.
Objective-C
[self.datePicker addTarget:self action:@selector(datePickedValueChanged:)
forControlEvents:UIControlEventValueChanged];
Swift
self.datePicker.addTarget(self, action: Selector("datePickedValueChanged:"), forControlEvents: UIControlEvents.ValueChanged)
Then if the value selected is out of allowed selection, you can set the DatePicker to maximum allowed valued (or whichever you want)
Objective-C
- (void)datePickedValueChanged:(id)sender{
if (self.datePicker.countDownDuration > 5400.0f) { //5400 seconds = 1h30min
[self.datePicker setCountDownDuration: 60.0f]; //Defaults to 1 minute
}
}
Swift
func datePickedValueChanged (sender: UIDatePicker) {
if (self.datePicker.countDownDuration > 5400) { //5400 seconds = 1h30min
self.datePicker.countDownDuration = 60.0; //Defaults to 1 minute
}
}
-- Previous answer :
I leave previous answer for others using a UIDatePicker
in Date or DateAndTime mode, if that can help some people
You can set minimum and maximum date of your UIDatePicker
.
Here user can't select a time before present time, and just go ahead 1 hour and 30 minutes.
Any attempt to select another time will make the UIDatePicker
to automatically go back to an allowed time interval.
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];
Upvotes: 4
Reputation: 1552
While Niko's solution works, it's downside is that the count of hours remains large. If you want to actually limit the hours that are displayed, I built a custom picker for just that purpose. It's a subclass of UIPickerView
and it replicates the functionality of UIDatePicker in countDownTimer mode, while adding support to set maxTimeInterval
.
You use it like this:
GSTimeIntervalPicker *picker = [[GSTimeIntervalPicker alloc] init];
picker.maxTimeInterval = (3600 * 3); // set the limit
picker.minuteInterval = 5; // the step. Default is 1 min.
picker.timeInterval = (3600 * 1.5); // 1 h 30 minutes
picker.onTimeIntervalChanged = ^(NSTimeInterval newTimeInterval) {
// Use the value
};
Available on GitHub under MIT license. Blog post here.
Upvotes: 12
Reputation: 4550
You can use NSTimer and set interval then update the NSDatePicker
declared a global variable int limiter = 0;
and the NSDate
holder for previous time;
add target to your NSDatePicker
[self.YourDatePicker addTarget:self action:@selector(dateChanged:)forControlEvents:UIControlEventValueChanged];
Set the interval to 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];
}
Sorry i can't continue this in detail i need to go home..
But hope this helps you.. Cheers!
Upvotes: 1