有没有人实现了一项功能,如果用户在一段时间内没有触摸屏幕,您会采取特定的操作?我正在努力找出最好的方法来做到这一点。
UIApplication 中有这个有点相关的方法:
[UIApplication sharedApplication].idleTimerDisabled;
如果你有这样的东西那就太好了:
NSTimeInterval timeElapsed = [UIApplication sharedApplication].idleTimeElapsed;
然后我可以设置一个计时器并定期检查该值,并在超过阈值时采取一些操作。
希望这能解释我在寻找什么。有没有人已经解决了这个问题,或者对如何做到这一点有任何想法?谢谢。
这是我一直在寻找的答案:
让您的应用程序委托子类 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 和idleTimer 是实例变量。
为了使其工作,您还需要修改 main.m 以告诉 UIApplicationMain 使用您的委托类(在本例中为 AppDelegate)作为主体类:
int retVal = UIApplicationMain(argc, argv, @"AppDelegate", @"AppDelegate");
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)