【发布时间】:2015-08-04 21:34:44
【问题描述】:
我试图通过更改 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++
}
}
}
【问题讨论】:
-
我删除了我的答案,因为我一开始不明白你的问题。为什么要以非预期的方式“隐藏”标签?你有自动布局吗?这可能是导致更改标签中心问题的原因。约束可能会保持在视图中。
-
是的,我使用自动布局,我删除它并尝试游戏,但没有发生同样的事情。当我开始游戏时,标签是隐藏的,但是当我单击按钮时,标签会显示出来,我正在玩井字游戏...
标签: ios iphone swift animation viewdidappear