Getting negative value when UISlider goes left

2019-08-28 22:37发布

问题:

I was trying to figure out how to get a negative value of the slider when it goes back.

When I slide from left to right it returns a positive value. I need it to return a negative value when it goes back (from right to left).

In other words, how could I detect that the slider is going back?

    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];

回答1:

A simple example expressing what @rmaddy already stated in his comment:

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