Gobinath Ed
Gobinath Ed

Reputation: 229

How to recognize the movement of the UISlider is from maximum to its minimum value and its Vice Versa?

I want to know how to identify the UISlider is tracking towards maximum to its minimum values? For ex, My slider maximum and minimum values 1.0 and 0.0. How can I identify my slider is tracking from 0.0 to 1.0 and vice versa?

Thanks!

Upvotes: 1

Views: 589

Answers (3)

if-else-switch
if-else-switch

Reputation: 977

There is an event of UISlider named value changed. Use that event and calculate the difference. If difference is positive then its going towards max value otherwise its moving towards min value. Use below action method.

- (IBAction)sliderdidchanged:(UISlider *)sender {
    if (sliderOldValue - sender.value > 0) {
      NSLog(@"Value Decreasing");
    }
    else {
     NSLog(@"Value Increasing");
    }
    sliderOldValue = sender.value;
}

Don't forget to make reference to this method from UISlider object in your xib or Storyboard. Also declare sliderOldValue as instance variable and in viewDidLoad assign its value to the slider's default value.

Upvotes: 1

.h

To track Last Value

@property(nonatomic,assign)    float lastValue;

.m

in ViewDidLoad listen for UIControlEventValueChanged

[yourSlider addTarget:self
                action:@selector(mySliderValueChanged:)
      forControlEvents:UIControlEventValueChanged];

Define the method

- (IBAction)mySliderValueChanged:(UISlider *)sender {

    float currentValue = sender.value;

    if (currentValue>self.lastValue) {

        NSLog(@"moving Right");
    }
    else{

        NSLog(@"moving Left");
    }

    self.lastValue = sender.value;
}

Upvotes: 0

Shai
Shai

Reputation: 25619

You can listen for the ValueChanged control event

[slider_ addTarget:self action:@selector(sliderMove:) forControlEvents:UIControlEventValueChanged];

...

- (void) sliderMove:(UISlider*)slider
{
    if (slider.value == slider.minimumValue) {
        // etc...
    }
    else if (slider.value == slider.maximumValue) {
        // so on and so forth
    }
}

Upvotes: 0

Related Questions