26

Apple's iPod (Music) app has a slider (UISlider, I presume) with variable scrubbing speeds: the further away from the slider you drag vertically, the smaller the proportion of your horizontal dragging speed that is reflected in the value change of the slider. Has anyone figured out how to duplicate this behavior?

matt
  • 515,959
  • 87
  • 875
  • 1,141

2 Answers2

33

Good question that inspired me to find a solution. I created a class named OBSlider that supports variable scrubbing speeds.

The solution in short: subclass UISlider, override -continueTrackingWithTouch:withEvent: and do the calculation of the change of self.value depending on the movement of the touch yourself. Thereby, you can freely influence the factor with which you want to multiply the touch movement to get to the new slider value.

For details, please read my blog post: OBSlider, a UISlider subclass with variable scrubbing speed.

Ole Begemann
  • 135,006
  • 31
  • 278
  • 256
  • Very nice. It doesn't quite do what the iPod app slider does - notice how, if you are dragging at a distance from the iPod app scrubber and you continue dragging upwwards towards the slider, the thumb moves towards you until it meets you at the slider. However, you've definitely shown what the values are that need to be manipulated, and this is what I was having trouble working out. (I too was overriding `continueTrackingWithTouch:withEvent:` and setting `self.value`, but I just wasn't getting the right behavior.) – matt Jan 04 '11 at 04:24
  • @matt: Yeah, I noticed. I'm not sure I like the iPod's behavior better, though. – Ole Begemann Jan 04 '11 at 11:43
  • @matt: I changed the code so it behaves more like the slider in the iPod app. – Ole Begemann Jan 05 '11 at 00:07
  • Very nice class! However, endTrackingWithTouch:withEvent: should send actions for UIControlEventTouchUpInside, instead of ValueChanged ;) – Cutetare Nov 18 '13 at 03:42
4

Nice work Ole ! I've made some minor modifications to get something more similar to the way iPod app slider's is working when sliding the finger closer to the UISlider ! Basically I handle a variable to track the value corresponding to the finger position, and if the user is getting closer, I apply a different formula (which is not perfect and could be improved a lot).

My fork is available here : http://github.com/sylverb/OBSlider

Sylverb
  • 939
  • 6
  • 14
  • @Sylverb: I did not see your answer here until a moment ago and I just made some modifications of my own to make it behave more like the iPod. Will review your changes now, maybe you want to have a look at mine. – Ole Begemann Jan 05 '11 at 00:09
  • 1
    @Ole Begemann: It looks still quite different from the iPod player slider : - the position modification should only occur when the user is getting closer to the slider. In your implementation, the modification is occuring when y coordinate changes - the slider modification seems to follow the scrubbingSpeed steps instead of beeing linear ! My modifications are not perfect yet (the formula to integrate the impact of y coordinate is not perfect), but it have a behaviour more similar to the one in ipod player ! – Sylverb Jan 05 '11 at 10:12
  • You are right, I should not change the value when dragging away from the slider. – Ole Begemann Jan 05 '11 at 11:03
  • It seems that you animated the position changes to "hide" the fact that the changes are following steps of scrubbingSpeed. IMO, you should use a more linear information fabsf(currentLocation.y - beganTrackingLocation.y) for example ;) – Sylverb Jan 05 '11 at 12:42
  • I'm not trying to hide anything. I'm still not convinced the iPod app uses a formula of the type you are using (I'm not sure I'm on the right track either). – Ole Begemann Jan 05 '11 at 16:02
  • I simply was of the impression that the iPod app used animations, too. – Ole Begemann Jan 05 '11 at 16:10
  • My formula is clearly not the one used by Apple (as already stated, I did a quick and dirty modification of your first formula just too have something working almost as Apple's slider) ! I'm using a linear formula while Apple's one looks more like an exponential one ! – Sylverb Jan 05 '11 at 16:27
  • @Sylverb: I incorporated your formula into my repository. Looks good, thanks! – Ole Begemann Jan 07 '11 at 14:03