代码之家  ›  专栏  ›  技术社区  ›  Mihir Mehta

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

  •  0
  • Mihir Mehta  · 技术社区  · 14 年前

    我尝试过用UIApplication子类化我的appDelegate类,但它给了我一个错误

    how to detect idle user in iphone-sdk

    如何解决该错误或以任何其他方式实现它。。。

    请帮忙

    谢谢

    2 回复  |  直到 8 年前
        1
  •  1
  •   user23743 user23743    14 年前

    好的,我已经回答了这个问题。但是,您可能需要考虑另一种方法,即 class_replaceMethod() UIView 使用自己的实现来接触方法。

        2
  •  0
  •   gjpc    12 年前

    这是一个在iOS4和iOS5上测试正常的探测器。这里有一个警告。如果您使用的是手势识别器,则当全局apptouch进入状态UIGestureRecognizerStateEnded时,必须将其设置为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