switch iphone uislider

iphone - switch - uistepper



Implementando pasos/ajustando UISlider (6)

Estoy tratando de implementar algún tipo de ajuste o pasos con el UISlider. He escrito el siguiente código pero no funciona tan bien como esperaba. Funciona, pero cuando la deslizo hacia arriba, toma 5 puntos a la derecha, dejando el dedo no centrado sobre el "círculo deslizante"

Este es mi código donde self.lastQuestionSliderValue es una propiedad de la clase que he establecido en el valor inicial del control deslizante.

if (self.questionSlider.value > self.lastQuestionSliderValue) { self.questionSlider.value += 5.0; } else { self.questionSlider.value -= 5.0; } self.lastQuestionSliderValue = (int)self.questionSlider.value;


¡Tal vez alguien lo necesite! En mi situación, necesitaba un paso entero, así que usé el siguiente código:

-(void)valueChanged:(id)sender { UISlider *slider = sender; slider.value = (int)slider.value; }


En realidad, es considerablemente más fácil de lo que pensé en un principio. Originalmente estaba tratando de obtener la propiedad de thumbrect y hacer matemáticas complicadas. Esto es lo que terminé con:

h Archivo:

@property (nonatomic, retain) IBOutlet UISlider* questionSlider; @property (nonatomic) float lastQuestionStep; @property (nonatomic) float stepValue;

m Archivo:

- (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; }

Asegúrese de conectar el método y la salida para su vista de UISlider y debería estar listo para continuar.


La solución más simple para mí fue solo

- (IBAction)sliderValueChanged:(id)sender { UISlider *slider = sender; slider.value = roundf(slider.value); }


Otro enfoque de Swift es hacer algo como

let step: Float = 10 @IBAction func sliderValueChanged(sender: UISlider) { let roundedValue = round(sender.value / step) * step sender.value = roundedValue // Do something else with the value }

Puede leer más sobre el enfoque y la configuración en mi post .


Una muy simple:

- (void)sliderUpdated:(UISlider*)sli { CGFloat steps = 5; sli.value = roundf(sli.value/sli.maximumValue*steps)*sli.maximumValue/steps; }

Excelente si quieres una solución rápida y has agregado el objetivo mediante UIControlEventValueChanged.


VERSIÓN SWIFT

Ejemplo: Desea que un control deslizante pase de 1 a 10000 en pasos de 100. La configuración de UISlider es la siguiente:

slider.maximumValue = 100 slider.minimumValue = 0 slider.continuous = true

En la acción func () para el control deslizante, use:

var sliderValue:Int = Int(sender.value) * 100