SpriteKit按钮保持按下状态

时间:2017-09-01 20:21:01

标签: ios swift sprite-kit

我做了两个按钮(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();
    }
}

1 个答案:

答案 0 :(得分:1)

按钮保持向下,因为按钮不再出现(即手指向上移动并离开屏幕)。相反,它发生在视图的其他部分。如果您希望按钮不被按下,请考虑将代码添加到touchesMoved方法中。