swift - 当我之前打开它后返回到 View Controller 时,它不会为标签设置动画 Xcode Swift

标签 swift xcode

当我打开我的应用程序时,我有一个从屏幕外动画的标签。当我第一次打开 View Controller 时,它起作用了。但是,当我转到另一个 View Controller ,然后返回到初始 View Controller 时,这个标签将只在那里,而不是动画。

    levelsLabel.center = CGPoint(x:levelsLabel.center.x - 500, y:levelsLabel.center.y)

    UIView.animate(withDuration: 2) {

        self.levelsLabel.center = CGPoint(x:self.levelsLabel.center.x + 500, y:self.levelsLabel.center.y)

    }

有人有什么建议吗?谢谢!

最佳答案

将您的代码块放在 viewWillAppear 而不是 viewDidLoad 中将使其工作。然而,根据Apple Doc .

viewDidAppear: Use this method to trigger any operations that need to occur as soon as the view is presented onscreen, such as fetching data or showing an animation.

以下是我建议您执行的操作。

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)

        levelsLabel.center = CGPoint(x:levelsLabel.center.x - 500, y:levelsLabel.center.y)
    }

    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)

        UIView.animate(withDuration: 2) {
            self.levelsLabel.center = CGPoint(x:self.levelsLabel.center.x + 500, y:self.levelsLabel.center.y)

        }
    }

关于swift - 当我之前打开它后返回到 View Controller 时,它不会为标签设置动画 Xcode Swift,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45527852/

相关文章:

ios - 如何在 Swift 中初始化 UIView?

ios - 为什么在 iOS 上使用 UserDefaults 存储敏感信息不安全?

swift - UITabBar 在 "Back"按钮后消失

xcode - 无法杀死的虚假 NSRunningApplication 坐在我的 Dock 中?

ios - 如何在 xcode 4 中部署 gdata-objectivec-client 库

ios - Xcode 9,我的 NSLog() 去哪儿了?未在 Xcode 控制台或 Console.app 中显示

objective-c - 从 Swift 隐藏 Objective-C 初始化器(或方法)

ios - 默认情况下 Alamofire 是否请求压缩响应?

Xcode 4 : Run tests from the command line (xcodebuild)?

ios - 如何在iOS中运行相互依赖的长时间运行任务