如何让我的胜利标签保持隐藏,直到用户获胜?

时间:2015-08-04 21:34:44

标签: ios iphone swift animation viewdidappear

我试图通过更改x和y参数来隐藏我的标签,但是当我开始玩游戏时,突然第一次点击标签出现我尝试了alpha方式,所有其他方式隐藏标签都很好,除了这个。

这是我的代码:

import UIKit

class ViewController: UIViewController {

    @IBOutlet weak var button1: UIButton!
    @IBOutlet weak var label: UILabel!
    @IBOutlet weak var imageView: UIImageView!


    var goNumber = 1

    var gameState = [0, 0, 0, 0, 0, 0, 0, 0, 0]

    let winnerCombination = [[0, 1, 2], [3, 4, 5], [6, 7, 8], [0, 3, 6], [1, 4, 7], [2, 5, 8], [0, 4, 8], [2, 4, 6]]

    var winner = 0


    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }


  override func viewDidAppear(animated: Bool) {
        self.label.center = CGPointMake(self.label.center.x - 400, self.label.center.y)

    }


    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
        @IBAction func buttonPressed(sender: UIButton) {

        if gameState[sender.tag] == 0 {
            var image = UIImage(named: "")
            if goNumber % 2 == 0 {
                image = UIImage(named: "cross.png")
                gameState[sender.tag] = 1
            }
            else{
                image = UIImage(named: "nought.png")
                gameState[sender.tag] = 2
            }
            for combination in winnerCombination {

                if (gameState[combination[0]] == gameState[combination[1]] && gameState[combination[0]] == gameState[combination[2]] && gameState[combination[0]] != 0) {

                    winner = gameState[combination[0]]

                    println("the winner is \(winner)")
                }
            }
            if winner != 0 {

                if winner == 1 {
                    label.text = "cross won"
                }
                else {
                    label.text = "nought won"
                }
                UIView.animateWithDuration(0.5, animations: {
                    self.label.center = CGPointMake(self.label.center.x + 400, self.label.center.y)

                    self.label.alpha = 1
                })
            }
            sender.setImage(image, forState: .Normal)
            goNumber++
        }
    }
}

1 个答案:

答案 0 :(得分:0)

只需使用UILabel的隐藏属性来显示和隐藏控件。

如果您想隐藏它,请致电

label.hidden = true

如果您想要显示它,请致电

label.hidden = false