·您当前的位置:首页 > 技术教程 > AS2与AS3技术 >

[AS3]又一个非常不错的ToolTip类源代码

时间:2016-02-25 09:52酷播
[AS3]又一个非常不错的ToolTip类源代码

[AS3]又一个非常不错的ToolTip类源代码

  1. package project.do93.pflip.view  
  2. {  
  3.       
  4.     import flash.accessibility.AccessibilityProperties;  
  5.     import flash.display.*;  
  6.     import flash.events.*;  
  7.     import flash.geom.Point;  
  8.     import flash.text.*;  
  9.     /** 
  10.      * @link kinglong@gmail.com 
  11.      * @author Kinglong 
  12.      * @version 0.1 
  13.      * @since 20090608 
  14.      * @playerversion fp9+ 
  15.      * 热区提示 
  16.      */   
  17.     public class ToolTip extends Sprite {  
  18.         private static var instance:ToolTip = null;  
  19.         private var label:TextField;          
  20.         private var area:DisplayObject;  
  21.         public function ToolTip() {  
  22.             label = new TextField();  
  23.             label.autoSize = TextFieldAutoSize.LEFT;  
  24.             label.selectable = false;  
  25.             label.multiline = false;  
  26.             label.wordWrap = false;  
  27.             label.defaultTextFormat = new TextFormat("宋体", 12, 0x666666);  
  28.             label.text = "提示信息";  
  29.             label.x = 5;  
  30.             label.y = 2;  
  31.             addChild(label);  
  32.             redraw();  
  33.             visible = false;  
  34.             mouseEnabled = mouseChildren = false;  
  35.         }  
  36.           
  37.         private function redraw() {  
  38.             var w:Number = 10 + label.width;  
  39.             var h:Number = 4 + label.height;              
  40.             this.graphics.clear();  
  41.             this.graphics.beginFill(0x000000, 0.4);  
  42.             this.graphics.drawRoundRect(3, 3, w, h, 5, 5);                
  43.             this.graphics.moveTo(6, 3 + h);  
  44.             this.graphics.lineTo(12, 3 + h);  
  45.             this.graphics.lineTo(9, 8 + h);  
  46.             this.graphics.lineTo(6, 3 + h);  
  47.             this.graphics.endFill();  
  48.             this.graphics.beginFill(0xffffff);  
  49.             this.graphics.drawRoundRect(0, 0, w, h, 5, 5);  
  50.             this.graphics.moveTo(3, h);  
  51.             this.graphics.lineTo(9, h);  
  52.             this.graphics.lineTo(6, 5 + h);  
  53.             this.graphics.lineTo(3, h);  
  54.             this.graphics.endFill();  
  55.         }  
  56.           
  57.         public static function init(base:DisplayObjectContainer) {  
  58.             if (instance == null) {  
  59.                 instance = new ToolTip();  
  60.                 base.addChild(instance);                  
  61.             }  
  62.         }  
  63.           
  64.         public static function register(area:DisplayObject, message:String):void {  
  65.             if(instance != null){  
  66.                 var prop:AccessibilityProperties = new AccessibilityProperties();  
  67.                 prop.description = message;  
  68.                 area.accessibilityProperties = prop;  
  69.                 area.addEventListener(MouseEvent.MOUSE_OVER, instance.handler);  
  70.             }  
  71.         }  
  72.           
  73.         public static function unregister(area:DisplayObject):void {  
  74.             if (instance != null) {  
  75.                 area.removeEventListener(MouseEvent.MOUSE_OVER, instance.handler);  
  76.             }  
  77.         }  
  78.           
  79.         public function show(area:DisplayObject):void {  
  80.             this.area = area;  
  81.             this.area.addEventListener(MouseEvent.MOUSE_OUT, this.handler);  
  82.             this.area.addEventListener(MouseEvent.MOUSE_MOVE, this.handler);  
  83.             label.text = area.accessibilityProperties.description;  
  84.             redraw();             
  85.         }  
  86.   
  87.           
  88.         public function hide():void {  
  89.             this.area.removeEventListener(MouseEvent.MOUSE_OUT, this.handler);  
  90.             this.area.removeEventListener(MouseEvent.MOUSE_MOVE, this.handler);  
  91.             this.area = null;  
  92.             visible = false;  
  93.         }  
  94.           
  95.         public function move(point:Point):void {               
  96.             var lp:Point = this.parent.globalToLocal(point);  
  97.             this.x = lp.x - 6;            
  98.             this.y = lp.y - label.height - 12;  
  99.             if(!visible){  
  100.                 visible = true;  
  101.             }  
  102.         }  
  103.           
  104.         private function handler(event:MouseEvent):void {  
  105.             switch(event.type) {  
  106.                 case MouseEvent.MOUSE_OUT:  
  107.                     this.hide();  
  108.                     break;  
  109.                 case MouseEvent.MOUSE_MOVE:  
  110.                     this.move(new Point(event.stageX, event.stageY));                     
  111.                     break;  
  112.                 case MouseEvent.MOUSE_OVER:  
  113.                     this.show(event.target as DisplayObject);  
  114.                     this.move(new Point(event.stageX, event.stageY))  
  115.                     break;  
  116.             }  
  117.         }  
  118.           
  119.     }  

调用:
 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");

热门文章推荐

请稍候...

保利威视云平台-轻松实现点播直播视频应用

酷播云数据统计分析跨平台播放器