/**
* 长按脚本
*/
export default class UILongPress extends Laya.Script {
/** @prop {name:longpressTime, tips:"长按时间", type:number, default:500}*/
public longpressTime: number = 500;
/** @prop {name:isOnceFlag, tips:"是否只触发一次长按", type:Boolean, default:true}*/
public isOnceFlag:Boolean = true;//是否只触发一次长按
public isMouseDown:Boolean = false;//是否鼠标按下
public lastMouseDownTime:number = 0;//上次鼠标按下时间
public ownerSprite:Laya.UIComponent;
public static stop:Boolean = false;
private mLongPressHandler:Laya.Handler;
private mClickHandler:Laya.Handler;
private mLongPressTriggleTime:number = 100;//长按时间统计变量,初始值给跟间隔一样,这样只要长按后就会立马触发一次
private mLongPressIntervalTime:number = 100;//判定长按后 每100ms触发一次clickhander
private mTarget:any;
public get clickHandler():Laya.Handler{
return this.mClickHandler;
}
public set clickHandler(value:Laya.Handler){
this.mClickHandler = value;
}
public get longPressHandler():Laya.Handler{
return this.mLongPressHandler;
}
public set longPressHandler(value:Laya.Handler){
this.mLongPressHandler = value;
}
onEnable():void {
this.ownerSprite = this.owner as Laya.UIComponent;
this.ownerSprite.on(Laya.Event.MOUSE_DOWN,this,this.onMouseDown);
}
onDisable():void {
this.mClickHandler = this.longPressHandler = null
}
onMouseDown(e:Laya.Event):void{
UILongPress.stop = false;
this.mTarget = e;
this.isMouseDown = true
this.lastMouseDownTime = Laya.Browser.now()
this.ownerSprite.on(Laya.Event.MOUSE_UP,this,this.onMouseUp)
this.ownerSprite.on(Laya.Event.MOUSE_OUT,this,this.onMouseOut)
}
onMouseUp(e:Laya.Event):void{
this.isMouseDown = false
this.mClickHandler && this.mClickHandler.runWith(e)
this.mTarget = null;
}
onMouseOut(e:Laya.Event):void{
this.isMouseDown = false
this.ownerSprite.off(Laya.Event.MOUSE_UP,this,this.onMouseUp)
this.ownerSprite.off(Laya.Event.MOUSE_OUT,this,this.onMouseOut)
this.mTarget = null
}
onUpdate():void{
if(UILongPress.stop && this.mTarget){
UILongPress.stop = false
this.onMouseOut(this.mTarget);
return;
}
if(this.isMouseDown && Laya.Browser.now()-this.lastMouseDownTime>this.longpressTime){
if(this.mLongPressTriggleTime>=this.mLongPressIntervalTime){
if(this.isOnceFlag){
this.cancelLongPress();
}
this.mLongPressHandler && this.mLongPressHandler.run();
this.mLongPressTriggleTime=0;
}else{
this.mLongPressTriggleTime += Laya.timer.delta;
}
}
}
//常用脚本组件尽量重写reset方法 可以在destroy后自动回收到对象池
onReset():void{
this.lastMouseDownTime=0;
this.isMouseDown=false;
this.longpressTime=500;
if(this.mLongPressHandler){
this.mLongPressHandler.recover();
this.mLongPressHandler=null;
}
if(this.mClickHandler){
this.mClickHandler.recover();
this.mClickHandler=null;
}
}
//当条件不满足时,要取消长按事件
cancelLongPress():void{
this.isMouseDown = false
this.ownerSprite.off(Laya.Event.MOUSE_UP,this,this.onMouseUp)
this.ownerSprite.off(Laya.Event.MOUSE_OUT,this,this.onMouseOut)
}
}
调用
const longpress:UILongPress = this.mBtnLongpress.getComponent(UILongPress);
longpress.isOnceFlag = false;
longpress.longPressHandler = Laya.Handler.create(this, this.OnBtnLongpress, null, false);
longpress.clickHandler = Laya.Handler.create(this, this.OnBtnLongpress, null, false);
private OnBtnLongpress():void{
}