2012-05-08 4 views
0

글로벌 터치 이벤트를들을 수있는 방법이 있습니까? 화면을 터치했을 때 알림을 받고 싶습니다. 지금은 내보기에서 TouchesBegan을 재정의하고 있지만 거품처럼 보이지는 않습니다. 즉, 버튼이나 키보드를 터치하면 메서드가 호출되지 않습니다. 어쩌면 NSNotificationCenter 또는 비슷한 것을 사용합니다.글로벌 접촉 이벤트가 있습니까?

감사합니다,

답변

1

내가 도움이 될 몇 가지 코드 here을 기록했다. 그러나 그 코드는 OP 코드와 함께 과 섞여 있으므로 여기에 깨끗한 버전이 있습니다.

당신이 할 수있는 일은 UIWindow를 서브 클래스 화하고이를 응용 프로그램 위임자의 윈도우로 전달하는 것입니다.

코드과 같이 보일 것이다 :

#import "MyKindOfWindow.h" 

@implementation MyKindOfWindow 

@synthesize touchDelegate = _touchDelegate; 

- (id)initWithFrame:(CGRect)aRect 
{ 
    if ((self = [super initWithFrame:aRect])) { 

     _touchDelegate = nil; 
    } 
    return self; 
} 

- (void)sendEvent:(UIEvent *)event 
{ 
    [super sendEvent: event]; 

    if (event.type == UIEventTypeTouches) 
     [_touchDelegate windowTouch:event]; 
} 

@end 

귀하의 AppDelegate 물론 MyKindOfWindowDelegate 프로토콜을 따라야합니다 (구현하는 것이

MyKindOfWindow.h

#import <UIKit/UIKit.h> 

@protocol MyKindOfWindowDelegate; 

@interface MyKindOfWindow : UIWindow 

@property (assign) id <MyKindOfWindowDelegate> touchDelegate; 

@end 

@protocol MyKindOfWindowDelegate <NSObject> 

@required 
- (void) windowTouch:(UIEvent *)event; 
@end 

MyKindOfWindow.m을 - (void) windowTouch:(UIEvent *)event 방법). 지금

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{ 
    self.window = [[MyKindOfWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; 
    [(MyKindOfWindow *)self.window setTouchDelegate:self]; //!!make your AppDelegate a delegate of self.window 

    //this part of code might be different for your needs 
    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) { 
     self.viewController = [[ViewController alloc] initWithNibName:@"ViewController_iPhone" bundle:nil]; 
    } else { 
     self.viewController = [[ViewController alloc] initWithNibName:@"ViewController_iPad" bundle:nil]; 
    } 

    self.window.rootViewController = self.viewController; 
    [self.window makeKeyAndVisible]; 
    return YES; 
} 
0

내가보기에 TouchesBegan을 무시 합니다만, 그것이 버블처럼되지 보일 수행합니다

didFinishLaunchingWithOptions:

과 같을 것이다.

당신이 맞아요 - 터치는보기 계층 구조의 루트 (즉, 창)에서 시작하여 터치 된보기가 발견 될 때까지보기 계층 구조를 따라 진행됩니다. UIView의 -hitTest:withEvent: 메소드를 살펴보십시오. 창에서 시작하여 해당 메서드가 히트 하위 뷰를 찾기 위해 호출 된 다음 하위 뷰에서 같은 메서드가 호출되는 식으로 계속됩니다.