难道一个方法被调用时UISlider
的value
变化?我希望能够基于UISliders
值更新值,但仅当它更改时...
谢谢你的帮助。
Answers:
在IB中,您可以将关联IBAction
到Value Changed event
或在代码中为添加目标/操作UIControlEventValueChanged
。例如:
[mySlider addTarget:self action:@selector(sliderValueChanged:) forControlEvents:UIControlEventValueChanged];
- (IBAction)sliderValueChanged:(UISlider *)sender {
NSLog(@"slider value = %f", sender.value);
}
请注意,如果你想要的值更改事件的用户sliding
,那么可以肯定的是,slider
的continuous property
也设置。另一方面,如果您不希望事件像用户一样触发,sliding
并且仅当事件完成滑动时才将其设置为NO(或在IB中取消选中)。
对于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
}
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];
}