LayaBox:按钮长按组件

2020-10-13  本文已影响0人  一眼就认出你

组件代码:

package UIEngine {
    import laya.components.Script;
    import laya.display.Sprite;
    import laya.events.Event;
    import laya.utils.Browser;
    import laya.utils.Handler;
    
    public class LongPress extends Script {
        /** @prop {name:longpressTime, tips:"长按时间", type:Int, default:500}*/
        /** @prop {name:isOnceFlag, tips:"是否只触发一次长按", type:Boolean, default:True}*/
        public var longpressTime: int = 500;
        public var isMouseDown:Boolean = false;//是否鼠标按下
        public var lastMouseDownTime:int = 0;//上次鼠标按下时间
        public var isOnceFlag:Boolean = true;//是否只触发一次长按
        private var _longPressHandler:Handler;
        private var _clickHandler:Handler;
        
        private var _longPressTriggleTime:int = 100;//长按时间统计变量,初始值给跟间隔一样,这样只要长按后就会立马触发一次
        private var _longPressIntervalTime:int = 100;//判定长按后 每100ms触发一次clickhander

        public static var _stop:Boolean = false;

        private var target:*;
        
        public function get clickHandler():Handler
        {
            return _clickHandler;
        }
        public function set clickHandler(value:Handler):void
        {
            _clickHandler = value;
        }
        public function get longPressHandler():Handler
        {
            return _longPressHandler;
        }
        public function set longPressHandler(value:Handler):void
        {
            _longPressHandler = value;
        }
        public var ownerSprite:*

        override public function onEnable():void {
            ownerSprite = this.owner
            ownerSprite.on(Event.MOUSE_DOWN,this,onMouseDown)
        }
        
        override public function onDisable():void {
            _clickHandler = longPressHandler = null
        }

        override public function onMouseDown(e:Event):void
        {
            LongPress._stop = false;
            this.target = e;
            isMouseDown = true
            lastMouseDownTime = Browser.now()
            ownerSprite.on(Event.MOUSE_UP,this,onMouseUp)
            ownerSprite.on(Event.MOUSE_OUT,this,onMouseOut)
        }

        override public function onMouseUp(e:Event):void
        {
            isMouseDown = false
            _clickHandler && _clickHandler.runWith(e)
            this.target = null;
        }

        override public function onMouseOut(e:Event):void
        {
            isMouseDown = false
            ownerSprite.off(Event.MOUSE_UP,this,onMouseUp)
            ownerSprite.off(Event.MOUSE_OUT,this,onMouseOut)
            this.target = null
        }

        override public function onUpdate():void
        {
            if(LongPress._stop && this.target){
                LongPress._stop = false
                this.onMouseOut(this.target);
                return;
            }
            if(isMouseDown && Browser.now()-lastMouseDownTime>longpressTime)
            {
                if(_longPressTriggleTime>=_longPressIntervalTime)
                {
                    if(isOnceFlag){
                        cancelLongPress();
                    }
                    _longPressHandler && _longPressHandler.run();
                    
                    _longPressTriggleTime=0;
                }
                else
                {
                    _longPressTriggleTime += Laya.timer.delta;
                }
            }
        }
        
        //常用脚本组件尽量重写reset方法 可以在destroy后自动回收到对象池
        public override function onReset():void
        {
            lastMouseDownTime=0;
            isMouseDown=false;
            longpressTime=500;
            if(_longPressHandler)
            {
                _longPressHandler.recover();
                _longPressHandler=null;
            }
            if(_clickHandler)
            {
                _clickHandler.recover();
                _clickHandler=null;
            }
        }
        
        //当条件不满足时,要取消长按事件
        public function cancelLongPress():void
        {
            isMouseDown = false
            ownerSprite.off(Event.MOUSE_UP,this,onMouseUp)
            ownerSprite.off(Event.MOUSE_OUT,this,onMouseOut)
        }
        
    }
}

页面使用:

//长按需求
var _compLongPress:LongPress = m_sendBtn.addComponent(LongPress);
_compLongPress.isOnceFlag=false;
_compLongPress.longPressHandler = Handler.create(this,onSendClick,[true],false);
_compLongPress.clickHandler = Handler.create(this,onSendClick,[false],false);//Handler的释放放到了comp 底层ondisable里面会清空跟该节点相关的所有handler
上一篇下一篇

猜你喜欢

热点阅读