How to show value of UISlider as Integer
The value of the UISlider currently shows as a float. How do I show it as an integer?
sliderCtl = [[UISlider alloc] initWithFrame:frame开发者_开发百科];
sliderCtl.minimumValue = 1;
sliderCtl.maximumValue = 15;
sliderCtl.continuous = NO;
sliderCtl.value = 1;
[sliderCtl setShowValue:YES];
if you want your slider to snap to different integer values, you could use something like this in your valueChanged-function:
-(IBAction)valueChanged:(UISlider*)sender {
int discreteValue = roundl([sender value]); // Rounds float to an integer
[sender setValue:(float)discreteValue]; // Sets your slider to this value
}
This will snap the slider after releasing your finger to values like 1,2,...,15 based on the distance between two numbers. If for instance you released the slider at 2.4, it'll snap back to 2, whereas at 2.6 it would snap to 3.
You could truncate or round the value from the slider and then update it to give the user feedback about the change (it would appear to snap to the integer value when the user releases).
精彩评论