我的目標是在任何視圖檢測我的應用程序內的任何觸摸事件...(即在我的應用程序應該檢測任何地方的觸摸事件...) 我已經嘗試過了通過繼承我的AppDelegate類與UIApplication的,但它給我的錯誤檢測任何觸摸事件在應用程序在iPhone
how to detect idle user in iphone-sdk
如何解決錯誤或以任何其他方式實現它...
請幫助
感謝
我的目標是在任何視圖檢測我的應用程序內的任何觸摸事件...(即在我的應用程序應該檢測任何地方的觸摸事件...) 我已經嘗試過了通過繼承我的AppDelegate類與UIApplication的,但它給我的錯誤檢測任何觸摸事件在應用程序在iPhone
how to detect idle user in iphone-sdk
如何解決錯誤或以任何其他方式實現它...
請幫助
感謝
OK,我已經回答了鏈接的問題。然而,你可能要考慮不同的方法,這是使用class_replaceMethod()
爲「調酒」的UIView
聯繫與您自己的實現方法。
下面是測試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
我正在使用iOS 7.1,並且在稍微不同的實現中使用它之後,我無法讓源UIView接收touchesBegan等來響應。我得到EXC_BAD_ACCESS碼= 2個=地址爲0x0上線iosBeginTouch(個體經營,@selector(的touchesBegan:withEvent :),觸摸,事件); – 2014-05-19 00:33:08
但我不需要爲每個視圖做這個...? – 2010-06-24 11:36:00
@mihirpmehta不,爲UIView類做一次。 – 2010-06-24 13:08:13
格雷厄姆嗨,你好,謝謝,但我在主窗口中的TabBar控制器和5的viewController 5個標籤......我不知道在哪裏使用class_replaceMethod()了出來...... – 2010-06-25 04:22:28