点击事件异步导致Web IOS 复制失败

因为Cocos点击事件是异步的,在IOS上复制需要同帧才能成功,请问大家有什么好办法吗。

    public copyToClipBoard(text: string) {
        let input = document.createElement("input");
        document.body.appendChild(input);
        input.value = text;
        input.select(); // 选择对象
        const result = document.execCommand("copy");
        document.body.removeChild(input);
        console.log("copyToClipBoard", result, text);
    }
// 此代码异步,失败
  this.node.on(Button.EventType.CLICK, this.copyToClipBoard, this);
// 此代码同步,正常
        window.addEventListener("click", function(event) {
            console.log("window click", event);
            playable.copyToClipBoard("+++++++");
        });

经过几小时的奋战(全程断点+追踪引擎代码),终于可以了,给后面有相同问题的同学一条活路吧。

onEnable(): void {

        this.uitransform = this.node.getComponent(UITransform);

        let pressed: boolean = false;

        game.canvas.addEventListener("touchstart", () => pressed = true);

        game.canvas.addEventListener("touchcancel", () => pressed = false);

        game.canvas.addEventListener("touchend", (event) => {

            if (pressed) pressed = false, this.checkHitNode(event.changedTouches[0]);

        }, { capture: true, passive: true });

        game.canvas.addEventListener("click", (event) => this.checkHitNode(event));

    }

    private checkHitNode(event: { clientX: number, clientY: number }): void {

        const canvas = game.canvas;

        const box = canvas.getBoundingClientRect();

        if (box == null) return;

        const relatedPos = { left: box.x, top: box.y, width: box.width, height: box.height };

        let location = view.convertToLocationInView(event.clientX, event.clientY, relatedPos, null);

        view["_convertPointWithScale"](location);

        let hit = this.uitransform.isHit(location);

        if (hit) this.copyToClipBoard();

    }

1赞

感谢你的分享,3.4.0 已经解决了这个问题,有相同问题的开发者可以下载 3.4.0 社区版测试。

1赞

可以讲一下,3.4.0后,这个问题是怎么解决的,到时有文档吗?