alskipp/ASValueTrackingSlider

Dates and times?

marcnicholas opened this issue · 3 comments

This would be terrific to select date/times. I tried a quick hack to see if I could pass NSDateFormatter, but the formatter expects a number :(

Any chance of adding a NSDateFormatter option? It's probably beyond my current abilities to do this.

Thx.

Hi @marcnicholas, thanks for your suggestion. In principle I think it would be possible to add date functionality to the pop up view, but it wouldn't be an entirely simple procedure.

UISlider is based on 2 main properties minimumValue and maximumValue which are floats. To add date functionality, 2 more properties would need to be added, minimumDate and maximumDate which would be NSDate instances.

Now, probably the simplest thing to do would be to calculate all the dates between minimumDate and maximumDate and store them in a NSArray instance variable _datesArray. The next thing to do is set minimumValue to 0 and set maximumValue to the count of _datesArray. When the slider moves you use its value to grab an NSDate instance from _datesArray and display the date via a NSDateFormatter.

Example: slider value is 27, grab item 27 from _datesArray, get a string value for the date by passing it through a NSDateFormatter, now pass your string to the popUpView to display.

After all that is set up I don't think the final result would be an improvement on the built in UIDatePicker. If you're only dealing with small date ranges a UISliderDatePicker might work successfully. As soon as you have a large range of dates to choose from the control will become difficult to use.

You could give it a try to see if it works for your needs, and if you do, best of luck. But due to the limitations I don't think I'd include the functionality in this control.

All the best,
Al

Hi @marcnicholas, a dataSource protocol has now been added to the control:

@protocol ASValueTrackingSliderDataSource <NSObject>
- (NSString *)slider:(ASValueTrackingSlider *)slider stringForValue:(float)value;
@end

It should enable you to add the functionality you requested. If you adopt the protocol in your controller and set yourself as the dataSource, you can supply any NSString value to the popUpView label that you want.

Perhaps something like the following:

- (void)viewDidLoad
{
  self.dateArray = @[NSDate0, NSDate1, NSDate2, NSDate3, NSDate4];
  self.dateFormatter = // init date formatter
  self.slider.minimumValue = 0;
  self.slider.maximumValue = 4;
}

- (NSString *)slider:(ASValueTrackingSlider *)slider stringForValue:(float)value;
{
    NSUInteger i = lroundf(value);
    return [self.dateFormatter stringFromDate:self.dateArray[i]];
}

Let me know if this helps.

Cheers,
Al

Hi Al,

Many thanks! Will give it a whirl when I get a minute.

Cheers,

-m