LayaBox脚本(TS):按钮缩放

2022-06-27  本文已影响0人  一眼就认出你
/**
 * 按钮缩放脚本
 */
export default class UIScaleComponent extends Laya.Script {
    /**
     * @prop {name:delayTime, tips:"", type:Int, default:100}
     */
    public  delayTime:number = 25;
    /**
     * @prop {name:scaleRadio, tips:"缩小系数", type:Number, default:0.9}
     */
    public  scaleRadio:number=0.9;
    private _oriScaleX:number=1;
    private _oriScaleY:number=1;

    onEnable():void {
        var com:Laya.UIComponent = this.owner as Laya.UIComponent;
        com.anchorX = 0.5;
        com.anchorY = 0.5;

        this._oriScaleX = com.scaleX;
        this._oriScaleY = com.scaleY;

        //添加鼠标按下事件侦听。按下时缩小
        com.on(Laya.Event.MOUSE_DOWN, this, this.scaleSmall);
        //添加鼠标抬起事件侦听。抬起时还原。
        com.on(Laya.Event.MOUSE_UP, this, this.scaleBig);
        //添加鼠标离开事件侦听。离开时还原
        com.on(Laya.Event.MOUSE_OUT, this, this.scaleBig);
    }

    private  scaleSmall():void{
        Laya.Tween.to(this.owner, {scaleX:this.scaleRadio, scaleY: this.scaleRadio}, this.delayTime);   //ui上使用自定义的缩放系数,以满足缩放系数较大的控件
    }

    private  scaleBig():void{
        //变大还原的缓动效果
        Laya.Tween.to(this.owner, {scaleX:this._oriScaleX, scaleY:this._oriScaleY}, this.delayTime);
    }
}
上一篇下一篇

猜你喜欢

热点阅读