我做了两个按钮(leftBtn和rightBtn)。在按下和按下时它们可以正常工作。但是,当您按住并移动手指远离按钮并按下时,按钮保持按下状态吗?

这是touchDown和touchUp的代码

func touchDown(atPoint pos : CGPoint) {
    let rightBtn = childNode(withName: "rightBtn");
    let leftBtn = childNode(withName: "leftBtn");

    if(rightBtn?.frame.contains(pos) == true){
        right = true;
    }
    else if(leftBtn?.frame.contains(pos) == true){
        left = true;
    }
}

func touchMoved(toPoint pos : CGPoint) {

}

func touchUp(atPoint pos : CGPoint) {
    let rightBtn = childNode(withName: "rightBtn");
    let leftBtn = childNode(withName: "leftBtn");

    if(rightBtn?.frame.contains(pos) == true){
        right = false;
        carController.stopMove();
    }
    else if(leftBtn?.frame.contains(pos) == true){
        left = false;
        carController.stopMove();
    }
}

最佳答案

该按钮保持按下状态是因为不再发生触摸(即手指向上移动并离开屏幕)的情况。相反,它发生在视图的其他部分。如果不想让按钮保持按下状态,请考虑将代码也添加到touchesMoved方法中。

关于ios - SpriteKit按钮保持按下状态,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/46007505/

10-14 20:49