Swift:如果按下按钮,如何在某些图片中输入?巨人if声明?

时间:2014-07-21 01:41:48

标签: swift

所以我有一个tic tac toe游戏,我需要帮助一个大的if语句基本上说:

If button 1 = pressed then feed this set of imgs into the line of code annotated below, else feed in these imgs

为6个左右的不同按钮执行此操作。我只需要物流方面的帮助,而且我是初学者,所以请清楚地解释所有建议。谢谢!

我的代码:

func setImgforCircle(spot:Int, player:Int) {
        var playerMark = player == 1 ? "Red_x" : "Red_o" 

if blueBtnClicked(sender: UIButton.self) = highlighted {  //This block does not work
            playerMark = player == 1 ? "Blue_x" : "Blue_o" 
        }

        plays[spot] = player
        switch spot {
        case 1:
            circle1.image = UIImage(named: playerMark)
        case 2:
            circle2.image = UIImage(named: playerMark)
        case 3:
            circle3.image = UIImage(named: playerMark)
        case 4:
            circle4.image = UIImage(named: playerMark)
        case 5:
            circle5.image = UIImage(named: playerMark)
        case 6:
            circle6.image = UIImage(named: playerMark)
        case 7:
            circle7.image = UIImage(named: playerMark)
        case 8:
            circle8.image = UIImage(named: playerMark)
        case 9:
            circle9.image = UIImage(named: playerMark)

        default:
            circle2.image = UIImage(named: playerMark)
        }

1 个答案:

答案 0 :(得分:0)

将您的圈子存储在一个数组中,然后您可以通过点访问正确的圈子:

var circles = [circle1,circle2,circle3,etc]
circles[spot].image = UIImage(named: playerMark)

您应该为图像创建命名系统。类似的东西:

<player>_<spot>_<type>

因此,示例图像名称将是

1_3_o

然后,您可以以编程方式创建图像名称:

var imageName = "\(player)_\(spot)_\(type)"
circles[spot].image = UIImage(named: imageName)

我不明白你试图用什么逻辑来确定哪种&#34;类型&#34;它是,但我想它应该基于谁来转。