AS3: SliderUI
UPDATE: Please use SliderUI v1.51 for the latest and greatest.
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.
31 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/
pretty buggy for me... i'm gonna switch to somthing else or spend an hour writing my own... but thanks anyways i think if i had managed to get some of the bugs ironed out i would have used it...
Hi Matt,
Thanks for your sharing. I have one question. I want to put content when the user is using this slider and dragging to the certain point, the content will show up on the stage. But I have no clue how to do that. Hope you could give me some direction. Thanks.
Hi Matt, Sorry I mean I want to cretae an effect like google map, using slider to control an image zoom in and zoom out. But I have no clude how to do that.
smile,
you can use this for the sliding behavior and then just have the currentValue be the actual scaleX and scaleY of the movie clip. set the minimum and maximum zooms in the method and its that easy.
this is probably a newbie question, so sorry in advance, ![]()
how do i get values from these sliders, say when i slide it to the right, i'd like a trace statement to tell me what its value is and then update a variable with that value
thank so much, your class is awsome!
@matt: you can use sliderInstance.percent if you want the percent or sliderInstance.currentValue which will give you the actual value.
very nice indeed! I modded it a little bit to add scroll wheel functionality and allowing you to click on the track to make it jump to that point. It is such a super well written class and so customizable! Really really top coding well done!
@Shaedo: Thanks for the kind words. I thought I updated this post with the link to v1.51 but I guess not. Make sure you're using that one for the latest files. added it to this post as well now.
Just some advice,
1) AS3 is not Perl or PHP, stop it.
2) You can improve the quality of your code by making it more self documenting.
@ Tony... Well spotted, As3 != Perl or PHP. Congratulations! go to the head of the class.... and out the door ... yep just keep going .... I tell you when to stop ....
[...] SliderUI von Matt Przybylski ist eine Klasse um Schieberegler (Slider) in Flash zu generieren. Somit können [...]
Awesome! Easiest way to create sliders i could find! Took me 5 minutes to create working sliders for controlling the volume on different sound channels.
Thumbs up!
How is the range:
this._range = (Math.abs(this._lowVal) + this._highVal);
If you take lowval = 1 and highval = 2 - this would give a range of 3, even though the range is 1 here.
@Rutger: I think the term range is a bit misleading here and I'm not sure why I called it that at the time. I'm looking at the latest version of the class and that code has since changed so I can't really answer that for you properly to be perfectly honest













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