当UISlider离开时获得负值

时间:2015-02-25 19:07:33

标签: ios objective-c uiview slider uislider

我试图弄清楚如何在滑块返回时获得滑块的负值。

当我从左向右滑动时,它返回一个正值。我需要它在返回时(从右到左)返回负值。

换句话说,我怎么能检测到滑块回来了?

    self.borderSlider = [[UISlider alloc] initWithFrame:CGRectMake(0, 0, 200, 30)];
    self.borderSlider.center = CGPointMake(self.view.centerX, SCREEN_WIDTH / 2);
    self.borderSlider.minimumValue = 0.0f;
    self.borderSlider.maximumValue = 100.0f;
    self.borderSlider.tintColor = [UIColor grayColor];
    [self.borderSlider addTarget:self action:@selector(borderSliderValueChanged:) forControlEvents:UIControlEventValueChanged];

2 个答案:

答案 0 :(得分:0)

一个简单的例子,表达了@rmaddy在评论中已经说过的内容:

@implementation YourClass {
  float _sliderValue;
}
- (void)borderSliderValueChanged:(UISlider *)slider {
  float currentValue = slider.value;
  if (currentValue > _sliderValue) {
    // right
  } else if (currentValue < _sliderValue) {
    // left
  }
  _sliderValue = currentValue;
}
@end

答案 1 :(得分:0)

我遇到了同样的问题,所以我开发了它,请检查以下仓库:Repo enter image description here

slider?.amplitude = 50 
slider?.sliderDidChange = { [weak slider] value in
        if value > 10 {
            slider?.activeColor = .blue
        } else if value < 0 {
            slider?.activeColor = .red
        } else {
            slider?.activeColor = .lightGray
        }
    }