I've got an array of arbitrary values along a UISlider
--values that I want to use as tick marks/stops using something like this approach but drawn in the slider frame itself. How can I dependably find or calculate the pixel/point positions of these values along the slider within the slider's frame?
Just to emphasize: unlike How to get the center of the thumb image of UISlider, I need the position within the frame of the slider.
Two important pieces of information to have are the pixel/point range of the thumb and the origin of this range. From that information it would be easy enough to calculate the point positions. Here's what I've tried; this code is called from viewDidLoad
:
CGRect bounds = slider.bounds;
CGRect trackRect = [slider trackRectForBounds:bounds];
//float thumbWidth = slider.currentThumbImage.size.width; // why does this give 0?
float thumbWidth = 23;
float thumb0 = 0.5 * thumbWidth; // left-most posn. of thumb center
float thumbRange = trackRect.size.width - thumbWidth; // point range of thumb
float sliderValueMin = slider.minimumValue;
float sliderValueWidth = slider.maximumValue - sliderValueMin;
float pointsPerValueUnit = thumbRange/sliderValueWidth;
Then
float tickPos = pointsPerValueUnit * (tickValue - sliderValueMin) + thumb0;
In theory, it should be correct, should it not? But when I plot the positions near the right-hand side, they are off by a few points. By fiddling around, I found a fudge that gives (what looks to be) the correct results... at least for my current slider configuration:
float thumbRange = trackRect.size.width - thumbWidth + 3.5;
I'd like my code to be reusable for different value ranges, slider sizes, for custom thumbs, etc., so I have two issues/questions:
- Why do I need this ~3.5 fudge factor? Where does it come from and how can I know whether it will be the same for all sliders?
- Why can't I get a value out of
slider.currentThumbImage.size.width
? This parameter has values later on in the app's execution--when the slider's value changes.* If not inviewDidLoad
, when should I draw the tick marks?
I would appreciate your help.
*The same holds true for thumbWidth
in
CGRect thumbPosition = [slider thumbRectForBounds:bounds trackRect:trackRect value:slider.value];
thumbWidth = thumbPosition.size.width;
EDIT
I appreciate the responses I've gotten so far, though judging from them it seems I didn't ask my questions precisely enough.
As I mention in the comments, my idea is to make this code reusable (and to make it available to the community), so the developer simply has to put the code in place and not have to fiddle around to find the fudge factor for his own slider. So let me clarify question #1:
1'. Is there some way to get the 3.5--or whatever number for a given slider configuration? Perhaps a parameter of the slider (similar to thumbWidth
), for example, so the tick marks will be correctly aligned automatically?
The UISlider
Class Reference is pretty sparse. Reading it, I would never have expected slider.currentThumbImage.size.width;
to be non-nil
. I'm thinking 3.5-ish fudge may be similarly hidden among the UISlider
's obscure properties.
Actually, re-reading what I've just written suggests a possible answer for question #2. Perhaps the thumb's width isn't set because the slider hasn't yet been drawn... could that be right?
bounds
? – nielsbot