在iOS应用程序中收听所有触摸事件
是否有可能以某种方式聆听并捕捉应用程序中发生的所有触摸事件?
我正在开发的应用程序将用于展示厅和信息亭,因此,如果在给定的几分钟内没有收到任何触摸,我将回到应用程序的开始部分。 一种屏保function,如果你愿意。 我打算通过在后台运行一个计时器来实现这一点,应该重置和重新启动每次触摸事件发生在应用程序的某个地方。 但是我怎样才能听触摸事件? 任何想法或build议?
这很容易。
你需要一个UIApplication
的子类(我们称之为MyApplication
)。
你修改你的main.m
来使用它:
return UIApplicationMain(argc, argv, @"MyApplication", @"MyApplicationDelegate");
return UIApplicationMain(argc, argv, @"MyApplication", @"MyApplicationDelegate");
你重写方法[MyApplication sendEvent:]
:
- (void)sendEvent:(UIEvent*)event { //handle the event (you will probably just reset a timer) [super sendEvent:event]; }
- (void)sendEvent:(UIEvent*)event { //handle the event (you will probably just reset a timer) [super sendEvent:event]; }
UIWindow
的子类可以用来做到这一点,通过重写hitTest:
然后在你的主窗口的XIB中,有一个对象通常简称为Window
。 单击该button,然后在实用程序窗格右侧转到标识(Alt-Command-3)。 在Class文本字段中,inputUIWindow
子类的名称。
MyWindow.h
@interface MyWindow : UIWindow @end
MyWindow.m
@implementation MyWindow - (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event { UIView *res; res = [super hitTest:point withEvent:event]; // Setup/reset your timer or whatever your want to do. // This method will be called for every touch down, // but not for subsequent events like swiping/dragging. // Still, might be good enough if you want to measure // in minutes. return res; } @end
您可以使用轻击手势识别器。 子类UITapGestureRecognizer
并导入<UIKit/UIGestureRecognizerSubclass.h>
。 这个定义了touchesBegan:
, touchesMoved:
, touchesEnded:
和touchesCancelled:
把你的触摸处理代码放在适当的方法中。
在application:didFinishLaunchingWithOptions:
实例化手势识别application:didFinishLaunchingWithOptions:
并将其添加到UIWindow
。 将cancelsTouchesInView
设置为NO
,它将透明地通过所有触摸。
信用: 这个职位 。
创build一个从UIApplication扩展的类“VApplication”,并将这些代码粘贴到相应的类
VApplication.h
#import <Foundation/Foundation.h> // # of minutes before application times out #define kApplicationTimeoutInMinutes 10 // Notification that gets sent when the timeout occurs #define kApplicationDidTimeoutNotification @"ApplicationDidTimeout" /** * This is a subclass of UIApplication with the sendEvent: method * overridden in order to catch all touch events. */ @interface VApplication : UIApplication { NSTimer *_idleTimer; } /** * Resets the idle timer to its initial state. This method gets called * every time there is a touch on the screen. It should also be called * when the user correctly enters their pin to access the application. */ - (void)resetIdleTimer; @end
VApplication.m
#import "VApplication.h" #import "AppDelegate.h" @implementation VApplication - (void)sendEvent:(UIEvent *)event { [super sendEvent:event]; // Fire up the timer upon first event if(!_idleTimer) { [self resetIdleTimer]; } // Check to see if there was a touch event NSSet *allTouches = [event allTouches]; if ([allTouches count] > 0) { UITouchPhase phase = ((UITouch *)[allTouches anyObject]).phase; if (phase == UITouchPhaseBegan) { [self resetIdleTimer]; } } } - (void)resetIdleTimer { if (_idleTimer) { [_idleTimer invalidate]; } // Schedule a timer to fire in kApplicationTimeoutInMinutes * 60 // int timeout = [AppDelegate getInstance].m_iInactivityTime; int timeout = 3; _idleTimer = [NSTimer scheduledTimerWithTimeInterval:timeout target:self selector:@selector(idleTimerExceeded) userInfo:nil repeats:NO]; } - (void)idleTimerExceeded { /* Post a notification so anyone who subscribes to it can be notified when * the application times out */ [[NSNotificationCenter defaultCenter] postNotificationName:kApplicationDidTimeoutNotification object:nil]; } @end
replace类名称“VApplication”给我们的
的main.m
像这样的文件
int main(int argc, char * argv[]) { @autoreleasepool { return UIApplicationMain(argc, argv, @"VApplication", NSStringFromClass([AppDelegate class])); } }
为相应的视图控制器注册一个通知
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationDidTimeout:) name:kApplicationDidTimeoutNotification object:nil];
一旦超时发生,通知将触发并处理这样的事件
- (void) applicationDidTimeout:(NSNotification *) notif //inactivity lead to any progress { }
您可以在视图层次结构的顶部放置一个透明视图,并在该视图中select是处理它接收的触摸事件,还是将它们传递给较低的视图。