【问题标题】:Is there a way to retrieve every responder that has handled a UITouch?有没有办法检索每个处理过 UITouch 的响应者?
【发布时间】:2009-12-18 17:50:51
【问题描述】:

我正在尝试在我的游戏中调试一些与 touchesBegan/Moved/Ended 相关的减速;我认为我的一些触摸响应器没有正确卸载,因此随着游戏的运行越来越多,它们堆叠起来并且触摸响应变得越来越慢,因为它们必须通过越来越大的响应器链。

有没有办法查看/检索 UITouch 在链中移动时所采用的路径?或者只是某种检索所有活动响应者列表的方法?

谢谢, -S

【问题讨论】:

    标签: iphone debugging uikit uitouch


    【解决方案1】:

    您可以在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() 是一个美丽的东西。当然要小心;它非常适合调试,但如果不加选择地使用会很混乱。

    【讨论】:

    • NSNotificationCenterself?
    • @zoul。抱歉,此代码来自劫持 NSNotificationCenter。接得好。固定。
    • 这似乎不适用于 iOS 3.2。声称 Method 不存在(以及您使用的其他反射函数)。知道为什么吗?
    • 确保#import 。我将编辑答案以包含它。
    【解决方案2】:

    我会查看您的内存使用情况,而不是尝试检索所有响应者。 要么查看仪器http://www.mobileorchard.com/find-iphone-memory-leaks-a-leaks-tool-tutorial/ 或者相反,只需将几个 NSLog(@"responded"); 放入您的 touchesBegan 方法中,然后查看每次触摸是否会记录十次。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-11-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多