听起来好像检测到键盘和其他地方的所有触摸就足够了。我们可以通过继承UIApplication
覆盖来做到这一点sendEvent:
。
我们将UIApplicationDelegate
使用新消息扩展协议application:willSendTouchEvent:
,并让我们的UIApplication
子类在处理任何触摸事件之前将消息发送给它的委托。
我的应用程序.h
@interface MyApplication : UIApplication
@end
@protocol MyApplicationDelegate <UIApplicationDelegate>
- (void)application:(MyApplication *)application willSendTouchEvent:(UIEvent *)event;
@end
我的应用程序.m
@implementation MyApplication
- (void)sendEvent:(UIEvent *)event {
if (event.type == UIEventTypeTouches) {
id<MyApplicationDelegate> delegate = (id<MyApplicationDelegate>)self.delegate;
[delegate application:self willSendTouchEvent:event];
}
[super sendEvent:event];
}
@end
我们需要让我们的应用委托符合MyApplicationDelegate
协议:
AppDelegate.h
#import "MyApplication.h"
@interface AppDelegate : UIResponder <MyApplicationDelegate>
// ...
AppDelegate.m
@implementation AppDelegate
- (void)application:(MyApplication *)application willSendTouchEvent:(UIEvent *)event {
NSLog(@"touch event: %@", event);
// Reset your idle timer here.
}
最后,我们需要让应用程序使用我们的新MyApplication
类:
主文件
#import "AppDelegate.h"
#import "MyApplication.h"
int main(int argc, char *argv[])
{
@autoreleasepool {
return UIApplicationMain(argc, argv,
NSStringFromClass([MyApplication class]),
NSStringFromClass([AppDelegate class]));
}
}