Slider with real time in Label

前端 未结 4 1249
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];
    }
    

提交回复
热议问题