Slider with real time in Label

2019-03-05 05:25发布

I need a slider for displaying real time in slider-label from 12-00 AM to 11-45 PM with step size 15 minutes. But I just have no idea how to do it. Сan anybody give some suggestions to me?

4条回答
虎瘦雄心在
2楼-- · 2019-03-05 05:39

You may use NSTimer to call a method every 15 minutes and update the slider/label with the current time.

You may want to take a look at NSDateFormatter, it may be useful to obtain proper string representation for your date.

查看更多
放荡不羁爱自由
3楼-- · 2019-03-05 05:45

Create a slider, and in the view controller, have the slider change the label as the value on the slider changed. To set the range of values, change the range in the Storyboard under the properties (you can set the change interval to steps of 15 there too). You may want to give it the interval in minutes or something similar and then just convert that to time :)

- (IBAction)sliderChanged:(UISlider *)sender
查看更多
姐就是有狂的资本
4楼-- · 2019-03-05 05:55

If you just need a time chooser with slider, use the following code in the sliderChanged: handler:

- (IBAction)onSliderChange:(id)sender {

    UISlider *slider = (UISlider *)sender;

    NSUInteger numberOfSlots = 24*4 - 1; //total number of 15mins slots

    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"h-mm a"];

    NSDate *zeroDate = [dateFormatter dateFromString:@"12-00 AM"];

    NSUInteger actualSlot = roundf(numberOfSlots*slider.value);
    NSTimeInterval slotInterval = actualSlot * 15 * 60;

    NSDate *slotDate = [NSDate dateWithTimeInterval:slotInterval sinceDate:zeroDate];
    [dateFormatter setDateFormat:@"h-mm a"];

    self.timeLabel.text = [dateFormatter stringFromDate:slotDate];
}
查看更多
啃猪蹄的小仙女
5楼-- · 2019-03-05 05:58

I'm adding another answer to add code. Yes, you can get the value from the slider and then set the text of the label after converting the value to a time. Like so:

// Add 0.5 to slider value and truncate it to an integer by type casting it as integer
int sliderIntValue = (int)([sender value] + 0.5f);

//do some conversion and set the label to the value

self.sliderLabel.text = newLabelText;
查看更多
登录 后发表回答