Buscar..


UISlider

C objetivo

Declare una propiedad de control deslizante en ViewController.h o en la interfaz de ViewController.m

@property (strong, nonatomic)UISlider *slider;

//Define frame of slider and add to view
CGRect frame = CGRectMake(0.0, 100.0, 320.0, 10.0);
UISlider *slider = [[UISlider alloc] initWithFrame:frame];
[slider addTarget:self action:@selector(sliderAction:) forControlEvents:UIControlEventValueChanged];
[self.slider setBackgroundColor:[UIColor clearColor]];
self.slider.minimumValue = 0.0;
self.slider.maximumValue = 50.0;
//sending a NO/False would update the value of slider only when the user is no longer touching the screen. Hence sending only the final value
self.slider.continuous = YES;
self.slider.value = 25.0;
[self.view addSubview slider];

Manejar el evento de cambio de control deslizante

- (IBAction)sliderAction:(id)sender {
    NSLog(@"Slider Value %f", sender.value);
}

Ejemplo de SWIFT

 let frame = CGRect(x: 0, y: 100, width: 320, height: 10)
 let slider = UISlider(frame: frame)
 slider.addTarget(self, action:  #selector(sliderAction), for: .valueChanged)
 slider.backgroundColor = .clear
 slider.minimumValue = 0.0
 slider.maximumValue = 50.0
 //sending a NO/False would update the value of slider only when the user is no longer touching the screen. Hence sending only the final value
 slider.isContinuous = true
 slider.value = 25.0
 view.addSubview(slider)

Manejando el evento de cambio deslizante

 func sliderAction(sender:UISlider!)
 {
    print("value--\(sender.value)")
 }

Añadiendo una imagen de pulgar personalizada

Para agregar una imagen personalizada para el pulgar del control deslizante, simplemente llame al método setThumbImage con su imagen personalizada:

Swift 3.1:

let slider = UISlider()
let thumbImage = UIImage
slider.setThumbImage(thumbImage, for: .normal)


Modified text is an extract of the original Stack Overflow Documentation
Licenciado bajo CC BY-SA 3.0
No afiliado a Stack Overflow