ios - 用户界面标签!出现而不是隐藏在 Swift 中

标签 ios swift

我试图在游戏获胜之前让我的标 checkout 现在屏幕上。当游戏获胜时,我希望标 checkout 现在屏幕上。我正在使用 UIView.animateWithDuration 和 viewDidAppear 方法。我认为这与 viewDidAppear 有关。

import UIKit

class ViewController: UIViewController {

    //All the odd numbers will be noughts & even will be crosses
    var goNumber = 1
    var winner = 0

    //0 = empty, 1 = nought, 2 = cross
    var gameState = [0, 0, 0, 0, 0, 0, 0, 0, 0]

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

    @IBOutlet weak var button0: UIButton!
    @IBOutlet weak var winnerLabel: UILabel!
    @IBOutlet weak var label: UILabel!

    @IBAction func buttonPressed(sender: AnyObject) {
        var image = UIImage()

        if gameState[sender.tag] == 0 {
            println(sender.tag)

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

            for combination in winningCombinations {
                if (gameState[combination[0]] == gameState[combination[1]] && gameState[combination[1]] == gameState[combination[2]]) && gameState[combination[0]] != 0 {
                    winner = gameState[combination[0]]
                }
            }

            if winner != 0 {
                println("We have a winner")

                label.text = (winner == 1) ? "Noughts has won" : "Crosses has won"

                UIView.animateWithDuration(1) {
                    self.label.center = CGPointMake(self.label.center.x + 400, self.label.center.y)
                }
            }

            goNumber++
            sender.setImage(image, forState: .Normal)
        }
    }

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

最佳答案

试试这个,

swift 2

UIView.animateWithDuration(1, delay: 0, options: CurveEaseOut, animations: {

    self.label.center = CGPointMake(self.label.center.x + 400, self.label.center.y)

}, completion : nil)

swift 3、4、5

UIView.animate(withDuration: 1, delay: 0, options: .curveEaseOut, animations: {

    self.label.center = CGPoint(x: self.label.center.x + 400, y: self.label.center.y)

}, completion : nil)

关于ios - 用户界面标签!出现而不是隐藏在 Swift 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32490949/

相关文章:

ios - 带有按钮 addTarget 事件的标签不会触发

c++ - 如何在 Objective-C 中使用初始化列表

objective-c - 更改 UITableViewCell 的选定状态

json - 解码 JSON 时出错。类型不匹配(Swift.Array<任何>,Swift.DecodingError

ios - 在 Swift 中将数据从 TableView 传递到选项卡栏 View Controller 。

ios - UIButton 问题

快速核心数据: save images from camera

ios - 有什么办法可以更改通知图像

ios - 如何使用 swift 比较核心数据中同一实体的两个属性值

xcode - 移除 App Store 对 Apple Watch 的支持