I’m trying to create a vertical toggle switch control for the iPhone (along the lines of UISwitch but vertical sliding).
I was wondering whether an existing control already existed or if there are any good tutorials that explain the basics of creating custom controls for the iPhone.
Currently I’ve tried using affine transforms to create the vertical switch from a basic UIswitch, and it works except that the slider is too small compared to the slider track, thus I’m looking for information on writing custom controls.
Any direction is much appreciated.
You could use a UIButton and two images to make it appear like a vertical toggle switch and swap images depending on switch state.
Subclass UIButton, add the switch state, image swapping, etc, use as needed.
EDIT — The other way to go is a completely custom control. You subclass UIControl and add functions similar to UISwitch‘s function (ie: initWithFrame, on, setOn:animated:).
You also need to send an event with the state changes, similar to what UISwitch does with:
You implement beginTrackingWithTouch, continueTrackingWithTouch and endTrackingWithTouch to slide the switch images as the touch moves across the switch. I did this to create a 2D slider. UISwitch also does its own localization, the ON/OFF changes to 0/1 for example.