【问题标题】:viewDidAppear() not being called when home button is pressed then launched again按下主页按钮然后再次启动时未调用 viewDidAppear()
【发布时间】:2017-07-13 23:59:26
【问题描述】:

我注意到在按下主屏幕然后再次启动时没有调用viewDidAppear。这就是为什么在我按下主页按钮然后再次启动我的应用程序后我的动画停止工作的原因。有没有办法解决这个问题?

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

    print("View did appear is launched")
    tapToPlayLabel.startBlink()
    settingsButton.startRotating()
}

【问题讨论】:

  • 你为什么期望 viewDidLoad 仅仅因为你按下了 Home 键就被调用了?
  • @Lawliet viewWillApear 如果您通过dismisspop 返回此视图控制器,将被触发。应用进入前台时不会触发。
  • 是的,他在点击主页按钮时说。谢谢你的收获。

标签: swift animation viewdidload


【解决方案1】:

首先,在通知中心注册,检测应用进入前台。

NotificationCenter.default.addObserver(self, selector: #selector(appMovedToForeground), name: Notification.Name.UIApplicationWillEnterForeground, object: nil)

然后在处理函数中做任何你想要的动画

func appMovedToForeground() {
    tapToPlayLabel.startBlink()
    settingsButton.startRotating()
}

【讨论】:

  • 您可以通过删除Notification.Name 并仅使用.UIApplicationWillEnterForeground 来缩短它。
  • @Fangming Ning 我把第一个代码放在哪里?在 viewDidLoad?
  • 代码有效,但我的动画仍然无法运行。我认为我的动画有问题。
  • @BlueB.Yup。这是设置代码,应该只发生一次
  • @BlueB。当然。如果对您有帮助,请将此问题标记为已回答。对于您的动画问题,如果您无法解决,请发布一个新问题,我们很乐意为您提供帮助
猜你喜欢
  • 2023-03-30
  • 2021-09-10
  • 2019-08-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多