AS3: SliderUI
View Example
View Documentation
Download Class & Example Files
If you're anything like me, you hate using components and try to steer clear of them WHENEVER you can. The SliderUI was spawned from this hate and is a super easy way to create a slider (the equivalent to the Slider component, except it weighs in at a whopping 1KB compared to the 17KB of the component). The SliderUI has pretty much all of the same functionality of the Slider component.
There is a start and end value you can set for the slider to match up with any "unit of measure" you want to use. You can also set where the slider should begin (value-wise) and ou can get back the position of the slider as the current value or the percentage value.
Here is the class:
-
package com.reintroducing.ui
-
{
-
import flash.display.Sprite;
-
import flash.display.Stage;
-
import flash.events.MouseEvent;
-
import flash.events.TimerEvent;
-
import flash.geom.Rectangle;
-
import flash.utils.Timer;
-
import flash.utils.getQualifiedClassName;
-
-
/**
-
* The SliderUI is a class that allows you to quickly create sliders with tracks without the need to use components.
-
* There is a "percent" and a "currentValue" property that you can tap into to see what position the slider is at on the track.
-
* </ p>
-
* Please note that if you are using the SliderUI on the "y" axis your track's registration point needs to be on the
-
* bottom (NOT the top) and the slider will go up to raise the value and down to lower it. If you do not pay attention
-
* to this your slider will be "broken".
-
*
-
* @author Matt Przybylski [http://www.reintroducing.com]
-
* @version 1.0
-
*/
-
public class SliderUI
-
{
-
//- PRIVATE & PROTECTED VARIABLES -------------------------------------------------------------------------
-
-
private var _stage:Stage;
-
private var _track:Sprite;
-
private var _slider:Sprite;
-
private var _timer:Timer;
-
private var _percent:Number;
-
private var _lowVal:Number;
-
private var _highVal:Number;
-
private var _startVal:Number;
-
private var _currentVal:Number;
-
private var _range:Number;
-
private var _axis:String;
-
private var _changeProp:String;
-
-
//- PUBLIC & INTERNAL VARIABLES ---------------------------------------------------------------------------
-
-
-
-
//- CONSTRUCTOR -------------------------------------------------------------------------------------------
-
-
/**
-
* Creates an instance of the SliderUI with the given parameters. If the $startVal parameter is set to something
-
* higher than the $highVal or lower than the $lowVal parameter, the $startVal parameter is reset to one of those two values.
-
*
-
* @param $stage The stage that the track and slider are sitting on
-
* @param $axis The axis that the slider will be used on
-
* @param $track The track to be used for the slider
-
* @param $slider The object that will function as the slider
-
* @param $lowVal A number representing the low value of the slider
-
* @param $highVal A number representing the high value of the slider
-
* @param $startVal A number representing the value the slider should start at (default: 0)
-
*
-
* @return void
-
*/
-
public function SliderUI($stage:Stage, $axis:String, $track:Sprite, $slider:Sprite, $lowVal:Number, $highVal:Number, $startVal:Number = 0):void
-
{
-
this._stage = $stage;
-
this._axis = $axis;
-
this._track = $track;
-
this._slider = $slider;
-
this._lowVal = $lowVal;
-
this._highVal = $highVal;
-
this._startVal = $startVal;
-
-
this._changeProp = (this._axis == "x") ? "width" : "height";
-
this._range = (Math.abs(this._lowVal) + this._highVal);
-
this._slider.buttonMode = true;
-
this._timer = new Timer(10);
-
-
if (this._startVal <this._lowVal) this._startVal = this._lowVal;
-
if (this._startVal> this._highVal) this._startVal = this._highVal;
-
-
if (this._startVal <0)
-
{
-
this._percent = (Math.abs(this._lowVal + Math.abs(this._startVal)) / this._range);
-
}
-
else
-
{
-
this._percent = (Math.abs(this._lowVal - this._startVal) / this._range);
-
}
-
-
this._currentVal = (this._lowVal + (this._range * this._percent));
-
-
if (this._axis == "x")
-
{
-
this._slider[this._axis] = (this._track[this._axis] + (this._percent * this._track[this._changeProp]));
-
}
-
else
-
{
-
this._slider[this._axis] = (this._track[this._axis] - (this._percent * this._track[this._changeProp]));
-
}
-
-
-
this.initEvents();
-
}
-
-
//- PRIVATE & PROTECTED METHODS ---------------------------------------------------------------------------
-
-
/**
-
* Initializes the slider and timer events.
-
*/
-
private function initEvents():void
-
{
-
this._slider.addEventListener(MouseEvent.MOUSE_DOWN, handleMouseDown);
-
this._slider.addEventListener(MouseEvent.MOUSE_UP, handleMouseUp);
-
this._timer.addEventListener(TimerEvent.TIMER, updateInfo);
-
}
-
-
//- PUBLIC & INTERNAL METHODS -----------------------------------------------------------------------------
-
-
/**
-
* Enables the controls of the SliderUI.
-
*
-
* @return void
-
*/
-
public function enable():void
-
{
-
this.initEvents();
-
}
-
-
/**
-
* Disables the controls of the SliderUI.
-
*
-
* @return void
-
*/
-
public function disable():void
-
{
-
this._slider.removeEventListener(MouseEvent.MOUSE_DOWN, handleMouseDown);
-
this._slider.removeEventListener(MouseEvent.MOUSE_UP, handleMouseUp);
-
this._timer.removeEventListener(TimerEvent.TIMER, updateInfo);
-
}
-
-
/**
-
* Cleans up the SliderUI for garbage collection.
-
*
-
* @return void
-
*/
-
public function destroy():void
-
{
-
this.disable();
-
-
this._timer = null;
-
}
-
-
//- EVENT HANDLERS ----------------------------------------------------------------------------------------
-
-
/**
-
* Starts the dragging of the slider and starts the timer to dispatch percentage.
-
*/
-
private function handleMouseDown($evt:MouseEvent):void
-
{
-
if (this._axis == "x")
-
{
-
this._slider.startDrag(false, new Rectangle(this._track.x, this._slider.y, this._track.width, 0));
-
}
-
else
-
{
-
this._slider.startDrag(false, new Rectangle(this._slider.x, this._track.y, 0, -this._track.height));
-
}
-
-
this._timer.start();
-
-
this._stage.addEventListener(MouseEvent.MOUSE_UP, handleMouseUp);
-
}
-
-
/**
-
* Stops the slider dragging and timer.
-
*/
-
private function handleMouseUp($evt:MouseEvent):void
-
{
-
this._slider.stopDrag();
-
this._timer.reset();
-
-
this._stage.removeEventListener(MouseEvent.MOUSE_UP, handleMouseUp);
-
}
-
-
/**
-
* Updates the info of the slider's position.
-
*/
-
private function updateInfo($evt:TimerEvent):void
-
{
-
this._percent = Math.abs((this._slider[this._axis] - this._track[this._axis]) / this._track[this._changeProp]);
-
this._currentVal = (this._lowVal + (this._range * this._percent));
-
}
-
-
//- GETTERS & SETTERS -------------------------------------------------------------------------------------
-
-
/**
-
* Returns the percentage of the slider's position on the track, between 0 and 1.
-
*
-
* @return Number
-
*/
-
public function get percent():Number
-
{
-
return this._percent;
-
}
-
-
/**
-
* Returns the current value of the slider's position on the track.
-
*
-
* @return Number
-
*/
-
public function get currentValue():Number
-
{
-
return this._currentVal;
-
}
-
-
//- HELPERS -----------------------------------------------------------------------------------------------
-
-
public function toString():String
-
{
-
return getQualifiedClassName(this);
-
}
-
-
//- END CLASS ---------------------------------------------------------------------------------------------
-
}
-
}
If you found this post useful, please consider leaving a comment, subscribing to the feed, or making a small donation.
11 Comments
Hi,
It sure looks promising but is not fully working for me: I wanted to have a starting value of 30 and it does put the drag handle closer to the right side of the scroll bar not not quite there--it places some pixels to the left of the right edge of the bar. Basically, I want to create a date slider where the user can slide back 1 day or 2 day, up to 30 days back.
Any ideas?
Thanks and here is my code:
-------------------------
import com.digitalwatershed.SliderTick;
var sui:SliderTick = new SliderTick(stage, "x", track_mc, slider_mc, 1, 30, 30);
//var sui2:SliderTick = new SliderTick(stage, "y", track2_mc, slider2_mc, -2, 1, -1);
trace("FIRST PERCENT: " sui.percent " & VALUE: " sui.currentValue);
//trace("SECOND PERCENT: " sui2.percent " & VALUE: " sui2.currentValue);
I think I have found a way around it. In your Class, modified the line as per:
//this._slider[this._axis] = (this._track[this._axis] (this._percent * this._track[this._changeProp]));
this._slider[this._axis] = (this._track[this._axis] (this._track[this._changeProp]));
Now, it would be nice to show the TickBars too!
But thanks a bunch anyway. The Flash CS3's built-in Slider doesn't seem to allow the starting position and that had me stuck for a while.
meengla, i imagine it has something to do with your registration points and the way your clips are set up. the programming behind the SliderUI has worked very well for me through various tests I have run and you shouldn't have to edit it any. if you want to send me your FLA i can take a look at it. and what do you mean by showing tickbars? if you want to show little ticks on your track all you'd have to do is set up a clip that sits on top of the track that shows the tick marks, the way you design it is entirely up to you.
Meengla,
I think I ran into the same problem you described just now and have fixed it. I've updated the download files in the v1.5 update (not this pot, the 1.5 one) so you should grab those files and it will have the latest version with the correct range. Sorry about that.
Wonderful class, I really enjoy using it. It does everything I need other than update the slider through the current value. In other words, I would like to be able to set the currentValue and have the slider position updated accordingly. Otherwise, wonderful work and thank you ![]()
Ben,
It does have that functionality in version 1.5:
http://evolve.reintroducing.com/2008/08/14/as3/as3-sliderui-v15/
![]()











[...] - Make draggable sliders easily and quickly! 1 Minute Ago View Example View Documentation View Post/Download If you