我正在尝试在我的游戏中调试一些与 touchesBegan/Moved/Ended 相关的减速;我认为我的一些触摸响应器没有正确卸载,因此随着游戏的运行越来越多,它们堆积起来,并且触摸响应变得越来越慢,因为它们必须通过越来越大的响应器链。
有没有办法查看/检索 UITouch 在链中移动时所采用的路径?或者只是某种方式来检索所有活动响应者的列表?
谢谢,-S
您可以劫持所需的方法UIResponder
以添加日志记录,然后调用原始方法。这是一个例子:
#import <objc/runtime.h>
@interface UIResponder (MYHijack)
+ (void)hijack;
@end
@implementation UIResponder (MYHijack)
+ (void)hijackSelector:(SEL)originalSelector withSelector:(SEL)newSelector
{
Class class = [UIResponder class];
Method originalMethod = class_getInstanceMethod(class, originalSelector);
Method categoryMethod = class_getInstanceMethod(class, newSelector);
method_exchangeImplementations(originalMethod, categoryMethod);
}
+ (void)hijack
{
[self hijackSelector:@selector(touchesBegan:withEvent:) withSelector:@selector(MYHijack_touchesBegan:withEvent:)];
}
- (void)MYHijack_touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
NSLog(@"touches!");
[self MYHijack_touchesBegan:touches withEvent:event]; // Calls the original version of this method
}
@end
然后在你的应用程序的某个地方(我有时把它放在它main()
本身),只需调用[UIResponder hijack]
. 只要子类在某个时候UIResponder
调用,您的代码就会被注入。super
method_exchangeImplementations()
是一件美好的事情。当然要小心;它非常适合调试,但如果不加选择地使用会非常混乱。
我会查看您的内存使用情况,而不是尝试检索所有响应者。要么查看工具http://www.mobileorchard.com/find-iphone-memory-leaks-a-leaks-tool-tutorial/
或者相反,只需在NSLog(@"responded");
你的 touchesBegan 方法中加入一些,看看它是否被记录为十每次触摸的次数。