Reputation: 5972
So a UISlider works great to change/choose values but it just doesn't look that great. What i'd like to implement is more like a horizontally spinning dial. I already have all the graphics I need and i've already implemented the scroll view to slide my image left and right.
The problem at this point is that unlick in a UISlider where I can simply call slider.value
to get its value, I have no clue how to set a range of values (max and min) and how to get the specific value out of a UIScrollView as it's scrolling.
Upvotes: 1
Views: 630
Reputation: 100632
I have to admit to not being 100% sure how you're using a scrollview as a slider, but you can use the contentOffset property to get "The point at which the origin of the content view is offset from the origin of the scroll view."
You'll need to do some maths to turn that into the value you want, but it's simple stuff. Suppose your scroll view is horizontal and has can be scrolled up to 400px across. That is, if contentOffset is 0 then you're at the start and if contentOffset is 400px then you're at the end. How you've used the scrollview will dictate exactly how you work this out, if you're desperate then you can just print the value of contentOffset programmatically and observe the range empirically.
In that case, you can get the proportion of the distance across the scrollview you are at by something like:
CGFloat proportionOffset = scrollView.contentOffset/400.0f;
If you then want to model the range [startX, endX] then just work out:
CGFloat offset = startX + proportionOffset * (endX - startX);
proportionOffset ends up in the range 0 to 1, at 0 when contentOffset is 0 and at 1 when contentOffset is 400. If, say, startX is 15 and endX is 35 then endX - startX is 20. If you multiply a number in the range 0 to 1 by 20 then you get a number in the range 0 to 20. That's how far through your range you want to be. But your range doesn't necessarily start at 0, so you need to add on the value of startX.
For efficiency terms, it's smart to set yourself up as a delegate of the scrollview and check the new contentOffset only when you receive a scrollViewDidScroll:. Otherwise you're doing a lot of polling that simply isn't necessary and, at the absolute worst, severely impeding the CPU's ability to sleep (and hence reduce heat and save battery).
Upvotes: 3