ユーザーが一定時間画面に触れなかった場合に特定のアクションを実行する機能を実装した人はいますか? それを実現する最善の方法を考えています。
UIApplication には、これと多少関連のあるメソッドがあります。
[UIApplication sharedApplication].idleTimerDisabled;
代わりに次のようなものがあればいいでしょう:
NSTimeInterval timeElapsed = [UIApplication sharedApplication].idleTimeElapsed;
次に、タイマーを設定してこの値を定期的にチェックし、しきい値を超えたときに何らかのアクションを実行できます。
うまくいけば、私が探していたものがこれで説明されるでしょう。この問題にすでに取り組んだ人はいますか、または、どのように取り組むかについて何か考えはありますか? ありがとうございます。
ベストアンサー1
私が探していた答えはこれです:
アプリケーションにサブクラス UIApplication を委任します。実装ファイルで、sendEvent: メソッドを次のようにオーバーライドします。
- (void)sendEvent:(UIEvent *)event {
[super sendEvent:event];
// Only want to reset the timer on a Began touch or an Ended touch, to reduce the number of timer resets.
NSSet *allTouches = [event allTouches];
if ([allTouches count] > 0) {
// allTouches count only ever seems to be 1, so anyObject works here.
UITouchPhase phase = ((UITouch *)[allTouches anyObject]).phase;
if (phase == UITouchPhaseBegan || phase == UITouchPhaseEnded)
[self resetIdleTimer];
}
}
- (void)resetIdleTimer {
if (idleTimer) {
[idleTimer invalidate];
[idleTimer release];
}
idleTimer = [[NSTimer scheduledTimerWithTimeInterval:maxIdleTime target:self selector:@selector(idleTimerExceeded) userInfo:nil repeats:NO] retain];
}
- (void)idleTimerExceeded {
NSLog(@"idle time exceeded");
}
ここで、maxIdleTime と idolTimer はインスタンス変数です。
これを機能させるには、main.m を変更して、UIApplicationMain にデリゲート クラス (この例では AppDelegate) をプリンシパル クラスとして使用するように指示する必要もあります。
int retVal = UIApplicationMain(argc, argv, @"AppDelegate", @"AppDelegate");