iOS how to make slider stop at discrete points

后端 未结 5 2218
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-13 06:21

I would like to make a slider stop at discrete points that represent integers on a timeline. What\'s the best way to do this? I don\'t want any values in between. It woul

5条回答
  •  情歌与酒
    2020-12-13 06:37

    I did as Nathan suggested, but I also want to update an associated UILabel displaying the current value in real time, so here is what I did:

    - (IBAction) countdownSliderChanged:(id)sender
    {
        // Action Hooked to 'Value Changed' (continuous)
    
        // Update label (to rounded value)
    
        CGFloat value = [_countdownSlider value];
    
        CGFloat roundValue = roundf(value);
    
        [_countdownLabel setText:[NSString stringWithFormat:@" %2.0f 秒", roundValue]];
    }
    
    
    - (IBAction) countdownSliderFinishedEditing:(id)sender
    {
        // Action Hooked to 'Touch Up Inside' (when user releases knob)
    
        // Adjust knob (to rounded value)
    
        CGFloat value = [_countdownSlider value];
    
        CGFloat roundValue = roundf(value);
    
        if (value != roundValue) {
            // Almost 100% of the time - Adjust:
    
            [_countdownSlider setValue:roundValue];
        }
    }
    

    The drawback, of course, is that it takes two actions (methods) per slider.

提交回复
热议问题