2012-01-30 55 views
1

我想設置一個全局UITouch處理程序來調試所有正在通過系統的UITouch事件以及哪個視圖是目標等。我該如何實現這一點?如何跟蹤全球UITouches?

回答

2

使UIApplication一個子類覆蓋sendEvent:方法:

@interface MyApplication : UIApplication 
@end 

@implementation MyApplication 

- (void)sendEvent:(UIEvent *)event { 
    if (event.type == UIEventTypeTouches) { 
     NSLog(@"Touch event: %@", event); 
    } 
    [super sendEvent:event]; 
} 

@end 

main.m通過這個子類UIApplicationMain名稱:

#import <UIKit/UIKit.h> 
#import "AppDelegate.h" 
#import "MyApplication.h" 

int main(int argc, char *argv[]) 
{ 
    @autoreleasepool { 
     return UIApplicationMain(argc, argv, 
      NSStringFromClass([MyApplication class]), 
      NSStringFromClass([AppDelegate class])); 
    } 
}