0

I have a UIButton called findButton that has a pulsing or breathing animation. When testing the app in the simulator and device, everything works perfectly until I click the Home Button while the animation is occurring. If I do press the Home Button and then reopen the app, the animation is no longer occurring.

I tried various answers found on this site and none have worked. I've tried using Extensions and NotificationCenter, but nothing that has supposedly worked for people in the past is working for me. Here's a look at the animation code that works perfectly before the Home Button is pressed:

override func viewWillAppear(_ animated: Bool) {

    UIView.animate(withDuration: 1.0,
                   delay: 0,
                   options: [.autoreverse, .repeat, .allowUserInteraction],
                   animations: {
                    self.findButton.transform = CGAffineTransform(scaleX: 1.175, y: 1.175)}, completion: nil)
} 

I'm wondering if perhaps I need to pause the animation before it goes into the background?

UPDATE: I was able to get the 'findButton' to work when pressed by moving the animations that occur AFTER it is pressed. I created a new VC and class and put findButton IBAction with the post-click animations there.

Unfortunately, findButton is still inanimate after returning from background. I am hoping that NoticationCenter will work now that I moved the post-click animations out of the first VC.

  • first of all call super.viewWillAppear inside the viewWillAppear – Manish_Nainwal Feb 22 '19 at 05:38
  • Please do not ask for a question to be exempt from closure. Someone may find a potential duplicate that you have missed, and if that happens, readers would like you to investigate the suggested link. – halfer Feb 22 '19 at 16:59
  • Mr. Nainwal, when I did your suggestion, get this error `Expression resolves to an unused function` –  Feb 22 '19 at 18:26

2 Answers2

2

@pooja's answer will definitely take you down the right path. But if you are using iOS 12, Xcode 10, Swift 4.2, then you will run into some errors when implementing NotificationCenter. Try @pooja's code, but make these changes:

NotificationCenter.default.addObserver(self, selector:#selector(doSomethingBefore), name: UIApplication.didEnterBackgroundNotification, object: nil)

NotificationCenter.default.addObserver(self, selector:#selector(doSomething), name: UIApplication.willEnterForegroundNotification, object: nil)
0

You can try registering for application did enter background

 NotificationCenter.default.addObserver(self, selector:#selector(doSomethingBefore), name: NSNotification.Name.UIApplicationDidEnterBackground, object: nil)

Register for app will become active

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

Set the transform for the button when view disappears

  @objc func doSomethingBefore(){
        findButton.transform = .identity
    }

Start animation when app becomes active

 @objc func doSomething(){
        self.view.layoutIfNeeded()
        UIView.animate(withDuration: 1.0,
                       delay: 0.3,
                       options: [.autoreverse, .repeat, .allowUserInteraction],
                       animations: {
                        self.findButton.transform = CGAffineTransform(scaleX: 1.175, y: 1.175)}, completion: nil)
        self.view.layoutIfNeeded()

    }

You can see a working sample here

Pooja Kamath
  • 1,290
  • 1
  • 10
  • 17
  • pooja, Thanks for the suggestion. I tried this before, with no success. But, I think that it might work now b/c I moved some other animations out of the VC and got `findButton` to work when pressed. See Update. –  Feb 22 '19 at 18:39