在iphone中检测应用程序中的任何触摸事件

时间:2010-06-24 10:13:47

标签: iphone events uiapplication

我的目标是在任何视图中检测“我的应用程序”中的任何触摸事件...(即在我的应用程序内部,应该检测到任何地方的任何触摸事件......) 我通过使用UIApplication继承我的appDelegate类来尝试它,但它给了我错误

how to detect idle user in iphone-sdk

如何解决该错误或以任何其他方式实施...

请帮助

由于

2 个答案:

答案 0 :(得分:1)

好的,我已经回答了这个相关问题。但是,您可能需要考虑一种不同的方法,即使用class_replaceMethod()使用您自己的实现“调动”UIView触摸方法。

答案 1 :(得分:0)

这是一个在IOS 4和5上测试好的探测器。有一个警告。如果您使用的是手势识别器,则必须在将状态UTestureRecognizerStateEnded指定为状态时将其设置为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