【问题标题】:Detect Any touch Event across Application in iphone检测iphone中跨应用程序的任何触摸事件
【发布时间】:2010-06-24 10:13:46
【问题描述】:

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

how to detect idle user in iphone-sdk

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

请帮忙

谢谢

【问题讨论】:

    标签: iphone events uiapplication


    【解决方案1】:

    好的,我已经回答了链接的问题。但是,您可能需要考虑另一种方法,即使用 class_replaceMethod()UIView 触摸方法与您自己的实现“混合”。

    【讨论】:

    • 但我不需要为每个视图都这样做吗...?
    • @mihirpmehta 不,为 UIView 类做一次。
    • 嗨格雷厄姆...谢谢,但我在主窗口中有 TabBar 控制器和 5 个用于 5 个选项卡的 viewController...我不确定在哪里使用 class_replaceMethod()...
    【解决方案2】:

    这是一个在 IOS 4 和 5 上测试正常的检测器。有一个警告。如果您使用手势识别器,则必须在它们进入 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
    

    【讨论】:

    • 我使用的是 iOS 7.1,在稍微不同的实现中使用它后,我无法让源 UIView 接收 touchesBegan 等响应。我在 iosBeginTouch(self, @selector(touchesBegan:withEvent:), touches, event); 行上得到 EXC_BAD_ACCESS code=2 address=0x0
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-05
    • 2010-10-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多