flex 时间滚动组件,强大(可自行选择时,分,秒,是否可用)

package hxht.comps.datefiled
{
    import flash.events.Event;
    import flash.events.MouseEvent;
    import flash.utils.clearTimeout;
    import flash.utils.setTimeout;
   
    import mx.core.UIComponent;
   
    import spark.components.supportClasses.ItemRenderer;
    import spark.components.supportClasses.SkinnableContainerBase;

 [Style(name="fillColors", type="Array" )]
 
    /**
     *  时间滚动组件
     * @author mmh
     */
    public class TimeStepper extends SkinnableContainerBase
    {
        /**
         *
         */
        public function TimeStepper()
        {
            super();
        }
  
  /**
   * 当前显示标签
   */  
  private var _currentTimeStr:String = "" ;

        [Bindable]
        [SkinPart(required="true")]
        /**
         *
         * @default
         */
        public var upBtn:UIComponent;

        [Bindable]
        [SkinPart(required="true")]
        /**
         *
         * @default
         */
        public var downBtn:UIComponent;

        [Bindable]
        [SkinPart(required="true")]
        /**
         *
         * @default
         */
        public var hourLabel:ItemRenderer;

        [Bindable]
        [SkinPart(required="true")]
        /**
         *
         * @default
         */
        public var minuteLabel:ItemRenderer;

        [Bindable]
        [SkinPart(required="true")]
        /**
         *
         * @default
         */
        public var secondLabel:ItemRenderer;

        //是否显示 小时
        private var _showHour:Boolean = true;

        //是否显示分钟
        private var _showMinute:Boolean = true;

        //是否显示秒
        private var _showSecond:Boolean = true;
  
        /**
         * 当前小时
         * @default
         */
        public var currentHour:Number = 0;

        /**
         * 当前分钟
         * @default
         */
        public var currentMinu:Number = 0;

        /**
         * 当前秒
         * @default
         */
        public var currentSecond:Number = 0;

        /**
         * 当前操作对象
         */
        public var currentSelected:int = 0;

        /**
         *
         * @default
         */
        public var currentItemRender:ItemRenderer;

        /**
         * 操作方向
         */
        protected var dire:int = 1;

        /**
         * 是否开始翻数
         */
        protected var isrun:Boolean = false;

        /**
         * 累加 速度
         */
        public var step:Number = 1;
  
  //catch id
        private var timeId:int = -1;

        override protected function createChildren():void
        {
//            if (!getStyle("skinClass"))
//                this.setStyle("skinClass", TimeStepperSkin);
            super.createChildren();

            if (hourLabel)
                selectedItemRender(hourLabel, 0);
            else if (minuteLabel)
                selectedItemRender(minuteLabel, 0);
            else if (secondLabel)
                selectedItemRender(secondLabel, 0);
            refreshLabel();
        }

        /**
         * 上翻
         * @param e
         *
         */
        protected function upBtnClickHandler(e:Event):void
        {
            dire = 1;

            isrun = true;
            timeFameHandler();
            timeId = setTimeout(startTimeHandler, 800);
        }


        /**
         * 下翻
         * @param e
         *
         */
        protected function downBtnClickHandler(e:Event):void
        {
            dire = -1;
            isrun = true;
            timeFameHandler();
            timeId = setTimeout(startTimeHandler, 800);
        }

  /**
   * 开始连续滚动
   *
   */  
        private function startTimeHandler():void
        {
            clearTimeout(timeId);
            timeId = -1;
            if (isrun)
                this.addEventListener(Event.ENTER_FRAME, timeFameHandler);
        }

        /**
         * 选中 小时
         * @param e
         */
        protected function hourSelectedHandler(e:Event):void
        {
            selectedItemRender(hourLabel, 0);
        }

        /**
         * 选中分钟
         * @param e
         */
        protected function minueSelectedHandler(e:Event):void
        {
            selectedItemRender(minuteLabel, 1);
        }

        /**
         * 选中秒
         * @param e
         */
        protected function secondSelectedHandler(e:Event):void
        {
            selectedItemRender(secondLabel, 2);
        }

        /**
         * 选中某个对象
         * @param item
         * @param type
         */
        protected function selectedItemRender(item:ItemRenderer, type:int):void
        {
            if (currentItemRender)
                currentItemRender.selected = false;
            item.selected = true;
            currentItemRender = item;
            currentSelected = type;
        }

        /**
         *
         * @param e
         */
        protected function outBtnClickHandler(e:Event):void
        {
            isrun = false;
            if (timeId > 0)
                clearTimeout(timeId);
            this.removeEventListener(Event.ENTER_FRAME, timeFameHandler);
        }

        /**
         * 滚动处理
         * @param e
         */
        protected function timeFameHandler(e:Event = null):void
        {
            if (isrun)
            {
                var _num:Number = 0;
                if (currentSelected == 0)
                {
                    _num = currentHour + dire * step;
                    if (_num >= 24)
                        currentHour = 0;
                    else if (_num < 0)
                        currentHour = 23;
                    else
                        currentHour = _num;
                }
                else if (currentSelected == 1)
                {
                    _num = currentMinu + dire * step;
                    if (_num >= 60)
                        currentMinu = 0;
                    else if (_num < 0)
                        currentMinu = 59;
                    else
                        currentMinu = _num;
                }
                else if (currentSelected == 2)
                {
                    _num = currentSecond + dire * step;
                    if (_num >= 60)
                        currentSecond = 0;
                    else if (_num < 0)
                        currentSecond = 59;
                    else
                        currentSecond = _num;
                }
                refreshLabel();
            }
        }

        /**
         * 刷新标签显示
         */
        protected function refreshLabel():void
        {
   updateLabel() ;//刷新标签
   
   var _str:String = "" ;
   if (showHour)
    _str += getZeroLabel(currentHour) ;
   
   if (showMinute)
    _str +=  (_str.length>0?(":"+getZeroLabel(currentMinu)):("00:"+getZeroLabel(currentMinu))) ;
   
   if (showSecond)
    _str +=  (_str.length>0?(":"+getZeroLabel(currentSecond)):("00:"+getZeroLabel(currentSecond))) ;
   
   currentTimeStr = _str ;//刷新 数据
        }
  
  protected function updateLabel( ):void
  {
   if (hourLabel)
    hourLabel.label = getZeroLabel(currentHour);
   
   if (minuteLabel)
    minuteLabel.label = getZeroLabel(currentMinu);
   
   if (secondLabel)
    secondLabel.label = getZeroLabel(currentSecond);
  }

        /**
         * 补零
         * @param num
         * @return
         */
        protected function getZeroLabel(num:int):String
        {
            return num < 10 ? ("0" + num) : num + "";
        }

        /**
         * 显示小时
         */
        public function get showHour():Boolean
        {
            return _showHour;
        }

        /**
         * @private
         */
        public function set showHour(value:Boolean):void
        {
            _showHour = value;
            showUI(hourLabel, _showHour);
        }

        /**
         * 显示分
         */
        public function get showMinute():Boolean
        {
            return _showMinute;
        }

        /**
         * @private
         */
        public function set showMinute(value:Boolean):void
        {
            _showMinute = value;
            showUI(minuteLabel, _showMinute);
        }

        /**
         * 显示秒
         */
        public function get showSecond():Boolean
        {
            return _showSecond;
        }

        /**
         * @private
         */
        public function set showSecond(value:Boolean):void
        {
            _showSecond = value;
            showUI(secondLabel, _showSecond);
        }

        override protected function partAdded(partName:String, instance:Object):void
        {
            super.partAdded(partName, instance);
            if (instance == upBtn)
            {
    upBtn.owner = this ;
    upBtn.buttonMode = true ;
                upBtn.addEventListener(MouseEvent.MOUSE_DOWN, upBtnClickHandler);
                upBtn.addEventListener(MouseEvent.MOUSE_OUT, outBtnClickHandler);
                upBtn.addEventListener(MouseEvent.MOUSE_UP, outBtnClickHandler);
            }
            else if (instance == downBtn)
            {
    downBtn.owner = this ;
    downBtn.buttonMode = true ;
                downBtn.addEventListener(MouseEvent.MOUSE_DOWN, downBtnClickHandler);
                downBtn.addEventListener(MouseEvent.MOUSE_OUT, outBtnClickHandler);
                downBtn.addEventListener(MouseEvent.MOUSE_UP, outBtnClickHandler);
            }
            else if (instance == hourLabel)
            {
                hourLabel.addEventListener(MouseEvent.CLICK, hourSelectedHandler);
    hourLabel.buttonMode = true ;
                showUI(hourLabel, _showHour);
            }
            else if (instance == minuteLabel)
            {
                minuteLabel.addEventListener(MouseEvent.CLICK, minueSelectedHandler);
    minuteLabel.buttonMode = true ;
                showUI(minuteLabel, _showMinute);
            }
            else if (instance == secondLabel)
            {
                secondLabel.addEventListener(MouseEvent.CLICK, secondSelectedHandler);
    secondLabel.buttonMode = true ;
                showUI(secondLabel, _showSecond);
            }
        }

        override protected function partRemoved(partName:String, instance:Object):void
        {
            if (instance == upBtn)
            {
                upBtn.removeEventListener(MouseEvent.MOUSE_DOWN, upBtnClickHandler);
                upBtn.removeEventListener(MouseEvent.MOUSE_OUT, outBtnClickHandler);
                upBtn.removeEventListener(MouseEvent.MOUSE_UP, outBtnClickHandler);
            }
            else if (instance == downBtn)
            {
                upBtn.removeEventListener(MouseEvent.MOUSE_DOWN, downBtnClickHandler);
                downBtn.removeEventListener(MouseEvent.MOUSE_OUT, outBtnClickHandler);
                downBtn.removeEventListener(MouseEvent.MOUSE_UP, outBtnClickHandler);
            }
            super.partRemoved(partName, instance);
        }

        /**
         *  显示隐藏某个组件
         * @param ui
         * @param flag
         *
         */
        protected function showUI(ui:UIComponent, flag:Boolean = true):void
        {
            if (ui)
            {
                ui.visible = flag;
                ui.includeInLayout = flag;
            }
        }
  
  /**
   * 设置 当前时间字符 02:01:20
   * @param value
   *
   */  
  public function set currentTimeStr( value:String ):void
  {
   if(_currentTimeStr!=value)
   {
    _currentTimeStr = value ;
    if(value)
    {
     var _arr:Array = value.split(":") ;
     if(_arr.length>=1)
      currentHour = int(_arr[0]) ;
     if(_arr.length>=2)
      currentMinu = int(_arr[1]) ;
     if(_arr.length>=3)
      currentSecond = int(_arr[2]) ;
    }
    else
    {
     currentHour = 0 ;
     currentMinu = 0 ;
     currentSecond = 0 ;
    }
    refreshLabel();
   }
  }
  
  [Bindable]
  public function get currentTimeStr():String
  {
   return _currentTimeStr;
  }

    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值