目标 C:是否可以通过一个类拦截所有用户操作? [英] Objective C: is it possible to intercept all user actions by one class?
问题描述
是否可以在我的应用的所有窗口上拦截所有用户操作,例如点击、滑动、输入文本等?
就像我在评论中所说的,子类 UIApplication
并覆盖实例方法 sendEvent:
.>
来自 文档,-sendEvent:
方法:
讨论
如果需要,可以通过以下方式拦截传入的事件子类化 UIApplication 并覆盖此方法.对于每个事件你拦截,你必须通过调用[super sendEvent:event]来调度它在您的实现中处理事件之后.
所以,它看起来像这样:
CustomUIApplication.h:
@interface CustomUIApplication:UIApplication- (void)sendEvent:(UIEvent *)event;@结尾
CustomUIApplication.m:
@implementation CustomUIApplication- (void)sendEvent:(UIEvent *)event{//...做你的事...[超级发送事件:事件];}@结尾
当然,您需要确保使用您的子类而不是默认的UIApplication
.这是关于如何在 Objective-C 中执行此操作的 Stack Overflow 答案,以及 这里是 Swift.
Is it possible to intercept all user actions like tap, swipe, enter text, etc. on all windows of my app?
Like I said in the comments, subclass UIApplication
and override the instance method sendEvent:
.
From the documentation for the UIApplication class, -sendEvent:
method:
Discussion
If you require it, you can intercept incoming events by subclassing UIApplication and overriding this method. For every event you intercept, you must dispatch it by calling [super sendEvent:event] after handling the event in your implementation.
So, it would look like this:
CustomUIApplication.h:
@interface CustomUIApplication:UIApplication
- (void)sendEvent:(UIEvent *)event;
@end
CustomUIApplication.m:
@implementation CustomUIApplication
- (void)sendEvent:(UIEvent *)event
{
// ...Do your thing...
[super sendEvent:event];
}
@end
Of course, you need to make sure your subclass is used instead of the default UIApplication
. Here is a Stack Overflow answer on how to do it in Objective-C, and here in Swift.
这篇关于目标 C:是否可以通过一个类拦截所有用户操作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!