我的目标是在任何视图检测我的应用程序内的任何触摸事件...(即在我的应用程序应该检测任何地方的触摸事件...) 我已经尝试过了通过继承我的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