iPhone: Detecting user inactivity/idle time since last screen touch
来源:互联网 发布:win下制作mac安装u盘 编辑:程序博客网 时间:2024/06/05 04:32
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 to do that.
There's this somewhat-related method in UIApplication:
[UIApplication sharedApplication].idleTimerDisabled;
It'd be nice if you instead had something like this:
NSTimeInterval timeElapsed = [UIApplication sharedApplication].idleTimeElapsed;
Then I could set up a timer and periodically check this value, and take some action when it exceeds a threshold.
Hopefully that explains what I'm looking for. Has anyone tackled this issue already, or have any thoughts on how you would do it? Thanks.
Solution:
Have your application delegate subclass UIApplication. In the implementation file, override the sendEvent: method like so:
- (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");}
where maxIdleTime and idleTimer are instance variables.
In order for this to work, you also need to modify your main.m to tell UIApplicationMain to use your delegate class (in this example, AppDelegate) as the principal class:
int retVal = UIApplicationMain(argc, argv, @"AppDelegate", @"AppDelegate");
Ref:
http://stackoverflow.com/questions/273450/iphone-detecting-user-inactivity-idle-time-since-last-screen-touch
- iPhone: Detecting user inactivity/idle time since last screen touch
- Detecting iPhone 6/6+ screen sizes in point values
- Real-Time Strategy Game with Touch Screen
- Touch Screen
- Touch screen
- No write since last change
- MTK6225 Idle Screen Digit Handler
- Time Out 与 idle
- Idle-Time Notification
- touch-screen with Timer
- Designing for Touch Screen
- ARM9(S3C2440) Touch Screen
- ARM9(S3C2440) Touch Screen
- Touch screen Calibration
- Using Touch Gestures 》Detecting Common Gestures
- 有关Last-Modified 与 If-Modified-Since
- Last-Modified 与If-Modified-Since辨析
- 有关Last-Modified 与 If-Modified-Since
- cassandra1.2的新功能
- Apache启动时无法加载php5apache2_2.dll
- 解释器模式(Interpreter)
- VS2010中使用JSONCPP方法
- 教你刷各大android手机应用市场下载排名
- iPhone: Detecting user inactivity/idle time since last screen touch
- Poj 1125 Stockbroker Grapevine
- 【AndroidManifest.xml详解】Manifest属性之sharedUserId、sharedUserLabel
- java中一个类想调用另一个类的变量
- [PowerDesign]数据库设计:需求模型(RQM)的简单介绍与案例教程(二)
- 威盾解密工具源代码
- 俄罗斯降下陨石雨 造成400多人受伤(图)
- 俄罗斯一家庭与世隔绝40年 不知有二战(图)
- 学习动力