ToolTip类的升级版
些ToolTip类确实比之前的方便多了,收藏
///////////////////////////------------------本文来自http://www.klstudio.com/post/198.html
这次做公司一个项目,发现原来的那个ToolTip类使用起来还是不方便,故我又重新写一个更方便更灵活的ToolTip类,现在把相关源代码也公布出来。
新ToolTip类
view plaincopy to clipboard
print
?
- package project.do93.pflip.view
- {
- import flash.accessibility.AccessibilityProperties;
- import flash.display.*;
- import flash.events.*;
- import flash.geom.Point;
- import flash.text.*;
- /**
- * @link kinglong@gmail.com
- * @author Kinglong
- * @version 0.1
- * @since 20090608
- * @playerversion fp9+
- * 热区提示
- */
- public class ToolTip extends Sprite {
- private static var instance:ToolTip = null;
- private var label:TextField;
- private var area:DisplayObject;
- public function ToolTip() {
- label = new TextField();
- label.autoSize = TextFieldAutoSize.LEFT;
- label.selectable = false;
- label.multiline = false;
- label.wordWrap = false;
- label.defaultTextFormat = new TextFormat("宋体", 12, 0x666666);
- label.text = "提示信息";
- label.x = 5;
- label.y = 2;
- addChild(label);
- redraw();
- visible = false;
- mouseEnabled = mouseChildren = false;
- }
- private function redraw() {
- var w:Number = 10 + label.width;
- var h:Number = 4 + label.height;
- this.graphics.clear();
- this.graphics.beginFill(0x000000, 0.4);
- this.graphics.drawRoundRect(3, 3, w, h, 5, 5);
- this.graphics.moveTo(6, 3 + h);
- this.graphics.lineTo(12, 3 + h);
- this.graphics.lineTo(9, 8 + h);
- this.graphics.lineTo(6, 3 + h);
- this.graphics.endFill();
- this.graphics.beginFill(0xffffff);
- this.graphics.drawRoundRect(0, 0, w, h, 5, 5);
- this.graphics.moveTo(3, h);
- this.graphics.lineTo(9, h);
- this.graphics.lineTo(6, 5 + h);
- this.graphics.lineTo(3, h);
- this.graphics.endFill();
- }
- public static function init(base:DisplayObjectContainer) {
- if (instance == null) {
- instance = new ToolTip();
- base.addChild(instance);
- }
- }
- public static function register(area:DisplayObject, message:String):void {
- if(instance != null){
- var prop:AccessibilityProperties = new AccessibilityProperties();
- prop.description = message;
- area.accessibilityProperties = prop;
- area.addEventListener(MouseEvent.MOUSE_OVER, instance.handler);
- }
- }
- public static function unregister(area:DisplayObject):void {
- if (instance != null) {
- area.removeEventListener(MouseEvent.MOUSE_OVER, instance.handler);
- }
- }
- public function show(area:DisplayObject):void {
- this.area = area;
- this.area.addEventListener(MouseEvent.MOUSE_OUT, this.handler);
- this.area.addEventListener(MouseEvent.MOUSE_MOVE, this.handler);
- label.text = area.accessibilityProperties.description;
- redraw();
- }
- public function hide():void {
- this.area.removeEventListener(MouseEvent.MOUSE_OUT, this.handler);
- this.area.removeEventListener(MouseEvent.MOUSE_MOVE, this.handler);
- this.area = null;
- visible = false;
- }
- public function move(point:Point):void {
- var lp:Point = this.parent.globalToLocal(point);
- this.x = lp.x - 6;
- this.y = lp.y - label.height - 12;
- if(!visible){
- visible = true;
- }
- }
- private function handler(event:MouseEvent):void {
- switch(event.type) {
- case MouseEvent.MOUSE_OUT:
- this.hide();
- break;
- case MouseEvent.MOUSE_MOVE:
- this.move(new Point(event.stageX, event.stageY));
- break;
- case MouseEvent.MOUSE_OVER:
- this.show(event.target as DisplayObject);
- this.move(new Point(event.stageX, event.stageY))
- break;
- }
- }
- }
- }
调用实例
view plaincopy to clipboard
print
?
- import project.do93.pflip.view.ToolTip;
- var base:Sprite = new Sprite();
- addChild(base);
- var sp:Sprite = new Sprite();
- sp.mouseEnabled=sp.mouseChildren=false;
- addChild(sp);
- //初始化ToolTip;
- ToolTip.init(sp);
- //与显示元素关联。
- ToolTip.register(xxx_mc, "http://www.klstudio.com");
- ToolTip.register(btn1, "我是按钮1");
- ToolTip.register(btn2, "我是按钮2");
演示效果
相关文件打包下载:http://www.klstudio.com/upload/tooltip.rar