单击swift后UIButton没有响应

时间:2017-02-17 16:36:34

标签: ios swift

我正在做四连胜游戏。我的主板上的每个位置都是UIButton,我试图制作一个功能,检查用户是否可以将芯片插入按下的按钮,当我的func返回false为一个按钮时,该按钮不再响应...谢谢         这是我的代码:

class ViewController: UIViewController {
//player1 = red, player2 = blue
var activePlayer = 1;
var activeGame = true;
var gameState:[Int] = [0,0,0,0,0,0,0,
                0,0,0,0,0,0,0,
                0,0,0,0,0,0,0,
                0,0,0,0,0,0,0,
                0,0,0,0,0,0,0,
                0,0,0,0,0,0,0];
let winningOptions = [[0,1,2,3]]; //just one option for now
@IBAction func btnClicked(_ gridBtns : UIButton){
    print(gridBtns.tag);
    let activePosition = gridBtns.tag - 1;
    if gameState[activePosition] == 0 && activeGame{
        gridBtns.center = CGPoint(x: gridBtns.center.x , y: gridBtns.center.y - 500);
        gameState[activePosition] = activePlayer;
        if okToPutChip(gridBtns){
            if activePlayer == 1{
                gridBtns.setImage(UIImage(named: "red_chip.png"), for: []);
                UIView.animate(withDuration: 0.5, animations: {
                    gridBtns.center = CGPoint(x: gridBtns.center.x, y: gridBtns.center.y + 500)
                });
                activePlayer = 2;
            }else{
                gridBtns.setImage(UIImage(named: "blue_chip.png"), for: []);
                UIView.animate(withDuration: 0.5, animations: {
                    gridBtns.center = CGPoint(x: gridBtns.center.x, y: gridBtns.center.y + 500)
                });
                activePlayer = 1;
            }
        }else{
            print("button will not show this again beacuse its not responding");
        }
        for option in winningOptions{
            if gameState[option[0]] != 0 && gameState[option[0]] == gameState[option[1]] && gameState[option[1]] == gameState[option[2]] && gameState[option[2]] == gameState[option[3]]{
                print("winner!!!!")
                activeGame = false;
            }
        }
    }
}
//my func to check if there is no chips blow this position
func okToPutChip(_ gridBtns : UIButton)->Bool{
    let position = gridBtns.tag-1;
    print("********position = \(position)")
    if position < 35 && gameState[position+7] == 0{
        print("now return false")
        return false;
    }else if position < 35 && gameState[position+7] != 0{
        print("here???")
        return true;
    }
    return true;
}

1 个答案:

答案 0 :(得分:0)

我猜测您是通过直接更改中心属性而遇到自动布局冲突。您可能会在日志中找到一些有趣的警告。出于某种原因,UIButton特别不喜欢在自动布局存在时手动移动。

关闭故事板文件上的“自动布局”或调整按钮的布局约束,而不是修改其中心属性。您可以查看有关如何为布局更改设置动画的一些教程。

或者你可以做一些琐事:

使用result % 2 // we can get single digit result / 2 | 0 // we can get tens digit, `| 0` can remove decimal. 并在BinaryNumber = result / 2 | 0 + result % 2 + '' // string concat 关闭中执行以下操作:

/**
 * @param {string} a
 * @param {string} b
 * @return {string}
 */
var addBinary = function(a, b) {
    var i = a.length - 1;
    var j = b.length - 1;
    var carry = 0;
    var result = "";
    while(i >= 0 || j >= 0) {
        var m = i < 0 ? 0 : a[i] | 0;
        var n = j < 0 ? 0 : b[j] | 0;
        carry += m + n; // sum of two digits
        result = carry % 2 + result; // string concat
        carry = carry / 2 | 0; // remove decimals,  1 / 2 = 0.5, only get 0
        i--;
        j--;
    }
    if(carry !== 0) {
        result = carry + result;
    }
    return result;
};

希望有所帮助。