As others have said, the existing API can't do it. If you want to do this, you'll have to write your own code to do it. I created a horizontal value picker in the past and it was annoying, but fairly straight forward. To do it I used a UILabel
, an image and a UIScrollView
.
First set up the UI
- Take the label and stick it in the scroll view. Populate the labels with the values you want.
- Set the scroll view height, width and position to show what you want the user to see. If you want the user to see the selected item and 3 on the top and 3 on the bottom then make it big enough to show 7 lines in the label.
- You image is the "selection indicator" you overlay this on top of the center item in the scroll view to show the user this is the selected item.
- If you want a pretty fade effect as your values go off the end you can create a white (or whatever background your app is) block which fades to transparent and overlay that over the top and bottom of the scroll view. This creates a fading number effect at the edges of the component.
Visually it show work... for the most part, but we need back end code to support it.
- Create a
UIScrollViewDelegate
and implement scrollViewDidScroll:
which will tell you the content offset of your scroll view when the users changes it.
- Write a method that will convert the scroll view content offset to which "item" was selected. If you know how many lines are in the label (and you should) and you know how tall the label is (and you should) take the label height/lines to get how tall each "item" is. Then you take the content offset, divide by the item height, floor it and you know which line your are at... while remembering the content offset is from the TOP of your scroll view, so add in half the height. :)
So the basic functionality is done and it works, but it's not pretty.
The selected item didn't end centered in my selection image, which is just visually ugly. But you can fix it easily enough. You know the height of the items in the list and you know which item is selected, so programmatically scroll the wheel to have that item centered.
The other issue I had was momentum. Like a real wheel I wanted my picker to have momentum so if you gave it a hard flick it would go for a while, but if you gave it a gentle flick it would stop pretty fast. I had long list and if people were going from 7 to 70 I wanted them to be able to do it easily. If you are interested in in that, I can show you how I did it and post some code when I get back to the machine with my code on it.