Slider with real time in Label

前端 未结 4 1119
Happy的楠姐
Happy的楠姐 2021-01-28 22:04

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 suggest

相关标签:
4条回答
  • 2021-01-28 22:32

    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];
    }
    
    0 讨论(0)
  • 2021-01-28 22:47

    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.

    0 讨论(0)
  • 2021-01-28 22:48

    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
    
    0 讨论(0)
  • 2021-01-28 22:51

    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;
    
    0 讨论(0)
提交回复
热议问题