0

私の目的は、任意のビューでアプリケーション内のタッチイベントを検出することです...(つまり、アプリケーション内の任意の場所のタッチイベントを検出する必要があります...)appDelegateクラスをUIApplicationでサブクラス化して試しましたが、エラー

iphone-sdkでアイドル状態のユーザーを検出する方法

そのエラーを解決する方法または他の方法で実装する方法...

助けてください

ありがとう

4

2 に答える 2

1

OK、リンクされた質問に答えました。ただし、独自の実装でタッチ メソッドをclass_replaceMethod()「スウィズル」するために使用する別のアプローチを検討することもできます。UIView

于 2010-06-24T10:32:00.363 に答える
0

これは、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
于 2012-06-23T20:08:32.540 に答える