UISlider值更改时会调用什么?


71

难道一个方法被调用时UISlidervalue变化?我希望能够基于UISliders值更新值,但仅当它更改时...

谢谢你的帮助。

Answers:


172

在IB中,您可以将关联IBActionValue Changed event或在代码中为添加目标/操作UIControlEventValueChanged。例如:

[mySlider addTarget:self action:@selector(sliderValueChanged:) forControlEvents:UIControlEventValueChanged];

- (IBAction)sliderValueChanged:(UISlider *)sender {
    NSLog(@"slider value = %f", sender.value);
}

请注意,如果你想要的值更改事件的用户sliding,那么可以肯定的是,slidercontinuous property也设置。另一方面,如果您不希望事件像用户一样触发,sliding并且仅当事件完成滑动时才将其设置为NO(或在IB中取消选中)。


7

对于Swift 3

//There is your slider
@IBOutlet weak var mSlider: UISlider!

//add a target on your slider on your viewDidLoad for example
self.mSlider.addTarget(self, action: #selector(MyClass.onLuminosityChange), for: UIControlEvents.valueChanged)

//callback called when value change
func onLuminosityChange(){
    let value = self.mSlider.value
}

0
ViewController.h

@property (strong, nonatomic) IBOutlet UIProgressView *prgs;

- (IBAction)slider:(id)sender;

@property (strong, nonatomic) IBOutlet UISlider *slider1;

@property (strong, nonatomic) IBOutlet UILabel *lbl;


ViewController.m

- (IBAction)slider:(id)sender 
{

    self.prgs.progress=self.slider1.value;

    self.lbl.text = [NSString stringWithFormat:@"%f", 
self.slider1.value];

}

1
您好Acharya,欢迎来到SO,谢谢您的回答。您能否编辑您的答案以描述这如何解决OP的问题?在此不建议仅使用代码的答案。谢谢!
蒂姆·马隆
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.