gpt4 book ai didi

ios - 点击快速后 UIButton 没有响应

转载 作者:行者123 更新时间:2023-11-30 12:40:27 25 4
gpt4 key购买 nike

我正在玩四连胜游戏。我板上的每个点都是一个 UIButton,我试图创建一个函数来检查用户是否可以将芯片插入按下的按钮,当我的函数对一个按钮返回 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;
}

最佳答案

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

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

或者你可以做一些卑鄙的事情:

使用 UIView.animate(withDuration:,animations:,completion:) 并在 completion 闭包中执行以下操作:

let superview = gridBtns.superview
gridBtns.removeFromSuperview()
gridBtns.translatesAutoresizingMaskIntoConstraints = false
superview.addSubview(gridBtns)

希望有帮助。

关于ios - 点击快速后 UIButton 没有响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42303024/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com