Switcher mobile skin for a toggle button
One of the components that is very common in a mobile app is the Switcher component. The on/off switch is usually available on IOS and Android devices. This component is like a toggle button but with a skin that makes it look as a Switcher.
Application
This is just a simple test and we have all the styles hardcoded on it.
<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark">
<fx:Style>
@namespace s "library://ns.adobe.com/flex/spark";
s|ToggleButton
{
skin-class: ClassReference("skins.SwitcherSkin");
background: Embed(source='/images/switcher_bg.png');
thumb: Embed(source='/images/switcher_thumb.png');
}
</fx:Style>
<s:ToggleButton x="50" y="50" width="94" height="27"/>
</s:Application>
I have a special skin, SwitcherSkin, for the toggle button that transforms the normal toggle to a switcher.
Skin
We are extending from MobileSkin to make our skin lightweight for a mobile project. And we only have 2 elements on it: the background that has the on/off background and the thumb that moves to cover the on/off labels.
package skins
{
import caurina.transitions.Tweener;
import flash.display.DisplayObject;
import spark.skins.mobile.supportClasses.MobileSkin;
public class SwitcherSkin extends MobileSkin
{
protected var background:DisplayObject;
protected var thumb:DisplayObject;
override protected function commitCurrentState():void
{
super.commitCurrentState();
invalidateDisplayList();
}
override protected function createChildren():void
{
background = addElement( "background" );
thumb = addElement( "thumb" );
}
protected function addElement( style:String ):DisplayObject
{
var elementAsset:Class = getStyle( style );
var element:DisplayObject;
if( elementAsset )
{
element = new elementAsset();
addChild( element );
}
return element;
}
override protected function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void
{
if( currentState == "up" )
{
Tweener.removeTweens( thumb );
Tweener.addTween( thumb, { time:0.4, x:0 } );
}
if( currentState == "upAndSelected" )
{
Tweener.removeTweens( thumb );
Tweener.addTween( thumb, { time:0.4, x:unscaledWidth - thumb.width } );
}
}
}
}
To make it more interesting, I added a little tween using the Tweener library . I hope you like it. You can improve on it making the labels dynamic and the dimensions too.
Raymond Camden
Florian
Nahuel Foronda
@Florian, I used it because of my old habits. In Flex 3x the default Flex engine was not good. I think that now Flex 4 do fine. You can use that if you prefer.
James Harvey
I agree with my comrade Ray, and I would gladly volunteer to contribute to such a task, as well as (if neccessary) assist and manage.
Arnaud Thorel
I see in Conqu a DateTime Picker, can you explain how do you this one ?
Thx
Arnaud
Nahuel Foronda
Arnaud Thorel
I tried to do it by myself using list and layout (like a vertical carrousel). I also have some trouble with the Scroller, but i succeed to get the selectedIndex at the center of the picker. Skin was more easier to do (gradient on the top and on the bottom).
Monkey Patch is a way of life. When the Flex SDK 4.0 I had to develop a messenger application in AIR using Gesture (looks like conqu - great job). But I patched list states to remove hovered state and add deleted state.
Yonas Kolb
Yonas Kolb
http://yonaskolb.com/blog/2012/1/18/flex-toggle-switch-spark-skin.html