animationuiviewiosuisliderpresentation-layer

querying the presentation layer of a UISlider


i am setting up a simple UISlider to animate it's progress:

[UIView animateWithDuration:songLength
         delay:0.0
       options:UIViewAnimationOptionRepeat|
         UIViewAnimationOptionAllowUserInteraction|
         UIViewAnimationOptionBeginFromCurrentState
       animations:^{
        [UIView setAnimationDuration:10.0];
        [myUISlider setValue:10.0];
       } completion:nil
   ];

  [UIView commitAnimations];

when the user presses a button i want to stop the animation at it's place.

i understand i need to query the presentation layer to figure out the value, however, the presentation layer is of type CALayer and not UISlider. hence, it has layer properties, like it's x/y position on the screen, but not the value of the slider itself.

it makes sense that by design the presentation layer can access all the current animated data of a layer, but i'm not sure how to work that out in code.

any ideas?


Solution

  • i figured out one way of resolving this matter, in case you are dealing with the same issue. by design there is no apparent way to query an animated thumb of a UISlider. animation querying works well for a layer, so one way is to create your own background and animated a layer that is the thumb.

    the way i worked around this is the use of NSTimer class. once i want the progress bar to start animating i set an NSTimer for 0.1s interval and call a function to update the thumb location. the animation before/after the thumb (as it progresses the left part is blue and what's remained is still white) is taken care of for you automatically.

    here is the code:

    updateTimer = [NSTimer scheduledTimerWithTimeInterval:.01 target:self selector:@selector(updateCurrentTime) userInfo:p repeats:YES];
    

    so every .01 of a second this method is called and it redraws the thumb, thus creating the animation. no need for an animation block.

    this works well and i am happy with the results. i am concerned with performance and will measure how resource intensive this implementation is. it maybe a good idea to go with the first option i've mentioned above.