2010-06-24 64 views

回答

1

OK,我已經回答了鏈接的問題。然而,你可能要考慮不同的方法,這是使用class_replaceMethod()爲「調酒」的UIView聯繫與您自己的實現方法。

+0

但我不需要爲每個視圖做這個...? – 2010-06-24 11:36:00

+0

@mihirpmehta不,爲UIView類做一次。 – 2010-06-24 13:08:13

+0

格雷厄姆嗨,你好,謝謝,但我在主窗口中的TabBar控制器和5的viewController 5個標籤......我不知道在哪裏使用class_replaceMethod()了出來...... – 2010-06-25 04:22:28

0

下面是測試OK上IOS 4和5有一個警告的檢測器。如果您使用的是手勢識別器,則必須在全局AppTouched設置爲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 
+0

我正在使用iOS 7.1,並且在稍微不同的實現中使用它之後,我無法讓源UIView接收touchesBegan等來響應。我得到EXC_BAD_ACCESS碼= 2個=地址爲0x0上線iosBeginTouch(個體經營,@selector(的touchesBegan:withEvent :),觸摸,事件); – 2014-05-19 00:33:08