AlterAnim.ts 3.13 KB
// Learn TypeScript:
//  - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

import { DummyNode } from "../CCDummyObjects";

const { ccclass, property,menu} = cc._decorator;
@ccclass
@menu("自定义UI组件/AlterAnim")
export default class AlterAnim extends cc.Component {

    @property({ type: Number, tooltip: "动画时长" })
    animTime: number = 0.3;

    @property({ type: cc.Node, tooltip: "弹出框遮罩" })
    maskNode: cc.Node = DummyNode;

    @property({ type: cc.Node, tooltip: "内容node父节点" })
    mainNode: cc.Node = DummyNode;

    onEnable() {
        this._showUIAnim(this.maskNode,this.mainNode);
    }

    /**
     * 
     * @param maskNode 遮罩node
     * @param mainNode 所有内容node
     * @param params callback:执行完成后的回调,maskNodeOpacityValue遮罩node的opacity值
     */
    _showUIAnim(maskNode: cc.Node, mainNode: cc.Node, params = { callback: () => { }, maskNodeOpacityValue: 200 }): void {
        maskNode.opacity = 0;
        maskNode.active = true;
        mainNode.scale = 0;
        mainNode.active = true;
        // 播放背景动画
        let opacityValue = params.maskNodeOpacityValue;
        cc.tween(maskNode)
            .to(this.animTime * 0.8, { opacity: opacityValue })
            .call(() => {
            })
            .start();
        // 播放主体动画
        cc.tween(mainNode)
            .to(this.animTime, { scale: 1 }, { easing: 'backOut' })
            .call(() => {
                // 弹窗已完全展示(动画完毕)
                if (params.callback) params.callback();
            })
            .start();

    }

    hideUIAnim(params){
        this._hideUIAnim(this.node,this.maskNode,this.mainNode,params);
    }

    /**
     * 
     * @param parentNode 该组件挂载的node
     * @param maskNode 遮罩node
     * @param mainNode 所有内容node
     * @param params callback:执行完成后的回调,maskNodeOpacityValue遮罩node的opacity值
     */
    _hideUIAnim(parentNode: cc.Node, maskNode: cc.Node, mainNode: cc.Node, params = { callback: () => { } }): void {
        // 拦截点击事件
        let blocker = parentNode.getChildByName("blocker");
        if (!blocker) {
            blocker = new cc.Node('blocker');
            blocker.addComponent(cc.BlockInputEvents);
            blocker.setParent(parentNode);
            blocker.setContentSize(parentNode.getContentSize());
        }
        blocker.active = true;
        // 播放背景动画
        cc.tween(maskNode)
            .delay(this.animTime * 0.2)
            .to(this.animTime * 0.8, { opacity: 0 })
            .call(() => {
            })
            .start();
        // 播放主体动画
        cc.tween(mainNode)
            .to(this.animTime, { scale: 0 }, { easing: 'backIn' })
            .call(() => {
                blocker.active = false;
                if (params.callback) params.callback();
            })
            .start();
    }
}