gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 02:02:15 26 4
gpt4 key购买 nike

当我打开我的应用程序时,我有一个从屏幕外动画的标签。当我第一次打开 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/

26 4 0