Programming a variable speed slider for iPhone
I have a slider with values from 0 to 100. I can easily stop at any number in the simulator but on my device the slider becomes a little jumpy and might move one or two ticks when you release your finger. I would like to have my slider act like the multispeed scrubber in the iPod, but I can't find a "prepackaged version" for the UISlider. I can't seem to capture the y coordinates of my touch once I have already grabbed the thumb 开发者_StackOverflow社区of the slider. Or maybe it is a sensitivity issue. Any help would be much appreciated. Thanks
If I get your meaning it seems like you want the thumb to move at variable speed with the relative y position of the touch. I haven't used the scrubber control.
The UISlider control inherits from UIResponder so you can subclass it and add your own
– touchesBegan:withEvent:
– touchesMoved:withEvent:
– touchesEnded:withEvent:
touchesMoved will be of special interest since it will give you the y coordinate. Don't forget to call super
and pass the events up the responder chain.
If you still get no events - maybe the thumb is in front and is intercepting them. Place a regular transparent UIView subclass over the slider, intercept touches and get coordinates there and again pass events up the chain.
I used this code once:
int progressAsInt = ((int)((completionSlider.value + 2.5f) / 5.0f) * 5);
[sender setValue:progressAsInt animated:YES];
in the slider's action function to make a UISlider set its thumb to discrete values only, like a volume control with detents. You could use different values decided by the y position of the touches you have detected to vary the granularity you allow, or to influence the amount of movement represented by actual touch events.
I'd write my own UISlider subclass and implement all the touch handling myself (i.e. in hitTest:withEvent:, return [self pointInside:point withEvent:event] ? self : nil;
, using UISlider just to do the rendering (you can get the relevant min/max rects by calling thumbRectForValue: or whatever it's called).
I'm not sure what you mean by "I can't seem to capture the y coordinates of my touch", but it's not (easily) possible to access a touch event once a subview has "captured" it. One hack is to handle all touches yourself but then forward touchesBegan/Moved/Ended/Cancelled.
精彩评论