mrEmpty
mrEmpty

Reputation: 841

iOS, trigger UIViewAnimation once at slider value n?

I have a set of sliders, I'm using Value Changed to feed a number to a % indicator. I'm also using this value to check if the slider is below a certain point. If it is, I want to run a UIViewAnimation (which I am, it's all working fine). However, the animation gets called constantly if the slider is moved below the threshold, meaning the items being animated go from point a to point b then back again over and over. So, can I trigger the animation once only at the threshold point?

This is how I get my value in pixels:

_sizeSliderRange = _sizeSlider.frame.size.width - _sizeSlider.currentThumbImage.size.width;
_sizeSliderOrigin = _sizeSlider.frame.origin.x + (_sizeSlider.currentThumbImage.size.width / 4.0);
_sizeSliderValueToPixels = (_sizeSlider.value * _sizeSliderRange) + _sizeSliderOrigin;

And I use a conditional inside the linked Value Changed IBAction function to checkt he value and do the work:

if (_sizeThumbX < 85) {  //if within 60px of the left margin we animate the label to sit float left
    [UIView transitionWithView:_sizeLabel duration:0.25f options:UIViewAnimationCurveEaseInOut animations:^(void) { etc etc

Thanks.

Upvotes: 1

Views: 240

Answers (2)

Abhishek Singh
Abhishek Singh

Reputation: 6166

Your code is working according to your logic that is everytime the slider value is changed and it is below 85 the animation will be invoked .You can have animation triggered only once in the follwing way :-

1>YOu can keep an absolute value at which animation occurs.Something like _sizeThumbX == 85

2>or you canhave counter of how many times the value changes . In a different function count and store the value of slider change.If the slider value lies in the 85 range do not increase the counter value and in your animation part check the counter flag and slider's current position if slider is still in below 85 range do not invoke animation if counter value is already 1 that is animation is already fired else invoke and increase the animation counter.

3>I am not ware of you conditions as you have not mentioned clearly but ithink you want to invoke the animation again if your slider goes beyond the range and comes back again , in that case make the count to zero (slider crosses the specified range) .

Upvotes: 1

David R&#246;nnqvist
David R&#246;nnqvist

Reputation: 56635

Like @Luis said, just use a BOOL property like this:

if (_sizeThumbX < 85) {  //if within 60px of the left margin we animate the label to sit float left
    if (!self.passedBelowThreshold) {
        [UIView transitionWithView:_sizeLabel duration:0.25f options:UIViewAnimationCurveEaseInOut animations:^(void) { /* ... */ }
    }
}
self.passedBelowThreshold = _sizeThumbX < 85;

Upvotes: 1

Related Questions