2010-06-24 6 views
0

내 목표는 내 응용 프로그램 내의 모든 터치 이벤트를 모든보기에서 감지하는 것입니다 (즉, 내 응용 프로그램 내에서 어떤 터치 이벤트도 감지되어야합니다 ...) 시도했습니다. UIApplication 내 AppDelegate에 클래스를 서브 클래스하지만iPhone에서 응용 프로그램의 모든 터치 이벤트 감지

이 제발 도와주세요 나에게 오류이 오류를 해결 또는 다른 방법으로 그것을 구현하는 방법을 how to detect idle user in iphone-sdk

을 ...주고있다

감사합니다.

답변

1

좋아요, 나는 그 연결된 질문에 대답했습니다. 그러나 class_replaceMethod()을 사용하여 UIView 터치 메소드를 사용자가 직접 구현하는 다른 방법을 고려할 수 있습니다.

+0

하지만 각보기마다 이렇게해야합니까? –

+0

@mihirpmehta 아니, UIView 클래스에 대해 한 번 수행하십시오. –

+0

안녕하세요 그레이엄 ... 고마워하지만 메인 윈도우와 5 viewController 5 탭에 TabBar 컨트롤러가 ... 나는 그들 중 class_replaceMethod()를 사용할지 모르겠다 ... –

0

여기 IOS 4 및 5에서 ok로 테스트 한 감지기가 있습니다. 경고가 있습니다. 제스처 인식기를 사용하는 경우 UIGestureRecognizerStateEnded 상태가되면 전역 AppTouched를 false로 설정해야합니다.

#import <objc/runtime.h> 

Boolean AppTouched = false;  // provide a global for touch detection  

static IMP iosBeginTouch = nil; // avoid lookup every time through 
static IMP iosEndedTouch = nil; 
static IMP iosCanedTouch = nil; 

// implement detectors for UIView 
@implementation UIView (touchesBeganDetector) 
- (void)touchesBeganDetector:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    AppTouched = true; 

    if (iosBeginTouch == nil) 
     iosBeginTouch = [self methodForSelector: 
           @selector(touchesBeganDetector:withEvent:)]; 

    iosBeginTouch(self, @selector(touchesBegan:withEvent:), touches, event); 
} 
@end 

@implementation UIView (touchesEndedDetector) 
- (void)touchesEndedDetector:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    AppTouched = false; 

    if (iosEndedTouch == nil) 
     iosEndedTouch = [self methodForSelector: 
           @selector(touchesEndedDetector:withEvent:)]; 

    iosEndedTouch(self, @selector(touchesEnded:withEvent:), touches, event); 
} 
@end 

@implementation UIView (touchesCancledDetector) 
- (void)touchesCancledDetector:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    AppTouched = false; 

    if (iosCanedTouch == nil) 
     iosCanedTouch = [self methodForSelector:  
           @selector(touchesCancledDetector:withEvent:)]; 

    iosCanedTouch(self, @selector(touchesCancelled:withEvent:), touches, event); 
} 
@end 

// http://stackoverflow.com/questions/1637604/method-swizzle-on-iphone-device 
static void Swizzle(Class c, SEL orig, SEL repl) 
{ 
    Method origMethod = class_getInstanceMethod(c, orig); 
    Method newMethod = class_getInstanceMethod(c, repl); 

    if(class_addMethod(c, orig, method_getImplementation(newMethod), 
           method_getTypeEncoding(newMethod))) 

     class_replaceMethod(c, repl, method_getImplementation(origMethod), 
             method_getTypeEncoding(origMethod)); 
    else 
     method_exchangeImplementations(origMethod, newMethod); 
} 

@interface touchDetector : NSObject {} 
- (id) init; 
@end 

@implementation touchDetector 

- (id) init 
{ 
    if (! [ super init ]) 
     return nil; 

    SEL rep = @selector(touchesBeganDetector:withEvent:); 
    SEL orig = @selector(touchesBegan:withEvent:); 
    Swizzle([UIView class], orig, rep); 

    rep = @selector(touchesEndedDetector:withEvent:); 
    orig = @selector(touchesEnded:withEvent:); 
    Swizzle([UIView class], orig, rep); 

    rep = @selector(touchesCancledDetector:withEvent:); 
    orig = @selector(touchesCancelled:withEvent:); 
    Swizzle([UIView class], orig, rep); 

    return self; 
} 
@end 
+0

iOS 7.1을 사용하고 약간 다른 구현에서 이것을 사용하면 touchesBegan 등을 수신하기 위해 소스 UIView를 가져올 수 없습니다. 나는 EXC_BAD_ACCESS 코드를 얻는다. = 2 address = 0x0 줄에 iosBeginTouch (self, @selector (touchesBegan : withEvent :), touches, event); –