iPhone: Detecting user inactivity/idle time since last screen touch

前端 未结 9 1267
我寻月下人不归
我寻月下人不归 2020-11-22 17:10

Has anybody implemented a feature where if the user has not touched the screen for a certain time period, you take a certain action? I\'m trying to figure out the best way t

9条回答
  •  隐瞒了意图╮
    2020-11-22 17:22

    I just ran into this problem with a game that is controlled by motions i.e. has screen lock disabled but should enable it again when in menu mode. Instead of a timer I encapsulated all calls to setIdleTimerDisabled within a small class providing the following methods:

    - (void) enableIdleTimerDelayed {
        [self performSelector:@selector (enableIdleTimer) withObject:nil afterDelay:60];
    }
    
    - (void) enableIdleTimer {
        [NSObject cancelPreviousPerformRequestsWithTarget:self];
        [[UIApplication sharedApplication] setIdleTimerDisabled:NO];
    }
    
    - (void) disableIdleTimer {
        [NSObject cancelPreviousPerformRequestsWithTarget:self];
        [[UIApplication sharedApplication] setIdleTimerDisabled:YES];
    }
    

    disableIdleTimer deactivates idle timer, enableIdleTimerDelayed when entering the menu or whatever should run with idle timer active and enableIdleTimer is called from your AppDelegate's applicationWillResignActive method to ensure all your changes are reset properly to the system default behaviour.
    I wrote an article and provided the code for the singleton class IdleTimerManager Idle Timer Handling in iPhone Games

提交回复
热议问题