C'est en fait considérablement plus facile que je pensais au départ. A l'origine, j'essayais d'obtenir la propriété thumbrect et de faire des calculs compliqués. Voici ce que j'ai fini avec:
h Fichier:
@property (nonatomic, retain) IBOutlet UISlider* questionSlider;
@property (nonatomic) int lastQuestionStep;
@property (nonatomic) int stepValue;
m Fichier:
- (void)viewDidLoad {
[super viewDidLoad];
// Set the step to whatever you want. Make sure the step value makes sense
// when compared to the min/max values for the slider. You could take this
// example a step further and instead use a variable for the number of
// steps you wanted.
self.stepValue = 25.0f;
// Set the initial value to prevent any weird inconsistencies.
self.lastQuestionStep = (self.questionSlider.value) / self.stepValue;
}
// This is the "valueChanged" method for the UISlider. Hook this up in
// Interface Builder.
-(IBAction)valueChanged:(id)sender {
// This determines which "step" the slider should be on. Here we're taking
// the current position of the slider and dividing by the `self.stepValue`
// to determine approximately which step we are on. Then we round to get to
// find which step we are closest to.
float newStep = roundf((questionSlider.value) / self.stepValue);
// Convert "steps" back to the context of the sliders values.
self.questionSlider.value = newStep * self.stepValue;
}
Assurez-vous de connecter la méthode et la prise pour votre vue UISlider et vous devriez être prêt à partir.