Search code examples
swiftanimationviewdidload

viewDidAppear() not being called when home button is pressed then launched again


I notice that viewDidAppear is not being called when the home screen is pressed and then launched again. This is why my animation stop working after I press the home button and then launch my app again. Is there any way to fix this?

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

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

Solution

  • First, register on notification center to detect app entering foreground.

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

    Then do whatever animation you want in the handler function

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