-1

am trying to make my label hidden by changing the x and y parameter but when i start playing the game and on the first click suddenly the label appear i tried the alpha way and all the other way to hide a label were fine except this one.

this my code:

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++
        }
    }
}
lapacino
  • 171
  • 1
  • 5
  • 15
  • I removed my answer as I did not understand your question at first. Why do you want to "hide" a label in a way that it is not intended? Do you have auto layout? This is probably what is causing the problem with changing the center of the label. The constraints might be keeping it in view. – Caleb Aug 04 '15 at 21:55
  • yes i use auto layout and i delete it and try the game without it same thing happen . when i start the game the label is hidden but when i click the button then the label show up am working on a tic tac toe game... – lapacino Aug 04 '15 at 22:05

1 Answers1

0

Just use the hidden attribute of UILabel to show and hide the control.

If you want to hide it, call

label.hidden = true

if you want to show it, call

label.hidden = false
Christian Abella
  • 5,747
  • 2
  • 30
  • 42