The exact moment iOS takes the view snapshot when entering background?

前端 未结 5 2069
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-13 09:57

I have a problem when putting my iPhone app to background by pushing the exit button, and then relaunching by tapping the launch icon on the home screen: the app\'s view doe

5条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-13 10:38

    I've now run some tests, and eliminated the problem, thanks to @Fabian Kreiser.

    To conclude: Kreiser had it right: iOS takes the screenshot immediately after the method applicationDidEnterBackground: returns -- immediately meaning, before the end of the current runloop.

    What this means is, if you launch any scheduled tasks in the didEnterBackground method you want to finish before leaving, you will have to let the current runloop run for as long as the tasks might take to finish.

    In my case, the scheduled task was an UIAnimateWithDuration method call -- I let myself be confused by the fact that both its delay and duration was 0 -- the call was nonetheless scheduled to run in another thread, and thus wasn't able to finish before the end of applicationDidEnterBackground method. Result: the screenshot was indeed taken before the display was updated to the state I wanted -- and, when relaunching, this screenshot flashed briefly onscreen, causing the unwanted flickering.

    Furthermore, to provide the "smooth" vs. "instant" transition behavior explained in my question, Kreiser's suggestion to delay the "smooth" transition call in applicationWillResignActive: and cancel the call in applicationDidEnterBackground: works fine. I noticed the delay between the two delegate methods was around 0.005-0.019 seconds in my case, so I applied a generous margin and used a delay of 0.05 seconds.

    My bounty, the correct answer tick, and my thanks go to Fabian. Hopefully this helps others in similar situation, too.

提交回复
热议问题