IOS: How do you prevent the screen from greying out immediately when re-enabling the idleTimer

StackOverflow https://stackoverflow.com/questions/7841080

  •  10-02-2021
  •  | 
  •  

سؤال

I'm working on an iOS game that primarily uses accelerometer input. Previous programmers set idleTimerDisabled=YES on startup and left it that way. I recently made a change such that the idle timer is only disabled during gameplay, and is re-enabled when a level ends.

The problem is, if the play time of the level is longer than the user's idleTimer setting, the screen will grey out the moment I set idleTimerDisabled=NO. Is there a way to reset the timer upon re-enabling so that the full time increment will occur before the idleTimer dims the screen?

هل كانت مفيدة؟

المحلول

Disable the idle timer on a timer, using something like this.

[self performSelector:@selector(enableIdleTimer) withObject:nil afterDelay:4];

- (void)enableIdleTimer { 

    [[UIApplication sharedApplication] setIdleTimerDisabled:NO];
}

Alternately, this is a more modern and simple approach:

dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(4 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
    [[UIApplication sharedApplication] setIdleTimerDisabled:NO];
});
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top