like Android toggle switch
The toggle switch in iOS looks great, and also supports Haptic feed back, so you might not get a use case to customise it. We can add custom animations, and also can support images or text on thumb control, etc. But this tutorial covers very basic control(Android like toggle) setup.
This how it looks :
Let’s start :
- Create an IBDesignable class for the control.
IBDesignableattribute helps to setup the UI Element through storyboards.
- Define the IBInspectable properties for colors and toggle status, etc that we need to access or set the values.
IBInspectableattributes help to configure the UI Element through storyboards & attribute inspector.
- Here I used draw method, to render the control. We can use helper methods also if customisation requires.
- And finally on touches ended, I am sending the notification for
valueChanged.by toggling the
onproperty on control. We can use the remaining touch event methods, to drag along with the touch, and for more customised state animations.
5. I provided the comments for the code, so you can understand what it does.