gpt4 book ai didi

iphone - 有没有办法检索每个处理过 UITouch 的响应者?

转载 作者:行者123 更新时间:2023-12-03 19:38:15 24 4
gpt4 key购买 nike

我正在尝试调试游戏中一些与 TouchBegan/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,您的代码就会被注入(inject)。

method_exchangeImplementations() 是一件美丽的事情。当然要小心;它非常适合调试,但如果不加区别地使用会非常困惑。

关于iphone - 有没有办法检索每个处理过 UITouch 的响应者?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1929740/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com