Domanda


I'm working on a Xcode project and I need to create a UISlider with 5 steps to rate something. I know this kind of slider exist natively because Apple uses it in Settings > General > Text Size. I can't post images but I hope you know what I'm speaking.

I searched on Google but I found nothing...
Thank you for your help !

PS: Sorry for my bad english but I'm french

È stato utile?

Soluzione

Firstly, never assume something is available, simply because it exists in an apple developed app. This is far from the truth. Sadly, they have held back a great many things.

To address your issue, try this (set slider min to 0 and max to 5 or whatever you want):

- (void)viewDidLoad 
{
    [super viewDidLoad];

    [slider addTarget:self action:@selector(sliderChanged:) forControlEvents:UIControlEventValueChanged];
}

This should allow the slider to pop to 0,1,2,3,4, and 5. (don't forget to replace my variable names with your own)

Hope that helps, happy coding.

- (void)sliderChanged:(UISlider *)mySlider { 
     mySlider.value = round(mySlider.value); 
     self.valueLabel.text = [NSString stringWithFormat:@"%g", mySlider.value]; 
}

Altri suggerimenti

Going off of the answer posted by @williamriley, after you enable the "popping" functionality, you can setup the code for each increment of the UISlider with the following code (inserted into the .m file):

- (IBAction)speedButton
{
   if(speed.value == 1)
   {
      // insert code here..
   }
}

You would create the button in the .h file, and assign it to the UISlider through a value changed assignment. Copy and paste this code 5 times for each increment of the slider. Obviously, there are more efficient ways of doing this, but this is just my two cents.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top