您可以使用 UIControlEventApplicationReserved 创建自定义事件吗?

发布于 2024-08-03 06:34:25 字数 562 浏览 2 评论 0原文

我编写了一个 UIControl 的子类,它跟踪许多我感兴趣的手势。在 UIControlEvents 枚举的文档中,它说有一系列名为 UIControlEventApplicationReserved 的事件号“可供应用程序使用”。这是否意味着我可以自由地将这个范围的数字用于我自己的自定义事件?

如果是这样,有人可以告诉我如何触发事件吗?我能想到的最明显的方法是:

enum {
     ...
     MyCustomEvent = 65,
     ...
};

...

UIEvent* customEvent;

...

for (id target in [self allTargets])
{
     for (NSString* action in [self actionsForTarget:target forControlEvent:MyCustomEvent])
     {
          [self sendAction:NSSelectorFromString(action) to:target forEvent:customEvent];
     }
}

这有效吗?

I have written a subclass of UIControl that tracks a number of gestures that are of interest to me. In the documentation for the UIControlEvents enumeration, it says that there is a range of event numbers called UIControlEventApplicationReserved that is "available for application use." Does this mean that I am free to use this range of numbers for my own custom events?

If so, can someone please tell me how to fire events? The obvious way I can think of to do it is this:

enum {
     ...
     MyCustomEvent = 65,
     ...
};

...

UIEvent* customEvent;

...

for (id target in [self allTargets])
{
     for (NSString* action in [self actionsForTarget:target forControlEvent:MyCustomEvent])
     {
          [self sendAction:NSSelectorFromString(action) to:target forEvent:customEvent];
     }
}

Would that even work?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一绘本一梦想 2024-08-10 06:34:25

好吧,这是一个老话题,但我要添加我的答案。尽管我对此表示怀疑,但我无法确定您是否可以在自己的应用程序中使用此掩码。

但我可以肯定地告诉你如何使用它。对于初学者,此值屏蔽位置 24、25、26 和 27 处的位。您应该编写自己的仅使用此位的枚举,例如:

enum {
    MyPrimaryActionEvent = 1 << 24,
    MySecondaryActionEvent = 1 << 25,
};

完成后,您可以注册这些操作:

[myButton addTarget:self action:@selector(someAction:) forControlEvents: MyPrimaryActionEvent];

每次操作 MyPrimaryActionEvent被触发时,self 将收到消息 someAction:。现在如何触发该操作取决于按钮本身。在您自己的 UIControl 子类中,您可以按如下方式触发更改:

[self sendActionsForControlEvents:MyPrimaryActionEvent];

这会将所有操作发送到为 MyPrimaryActionEvent 事件注册的所有目标。你就完成了。

Okay, this is an old subject but I'm going to add my answer to this. I can't really tell for sure whether you can use this mask for your own application even though I suspect it.

But I can tell you for sure how to use it. For starter this value masks the bits at position 24, 25, 26 and 27. You should write an enum of your own that uses this bits only, for example:

enum {
    MyPrimaryActionEvent = 1 << 24,
    MySecondaryActionEvent = 1 << 25,
};

Once that is done you can register for these actions:

[myButton addTarget:self action:@selector(someAction:) forControlEvents: MyPrimaryActionEvent];

Every time the action MyPrimaryActionEvent is triggered, self will receive the message someAction:. Now how to trigger that action is up to the button itself. In your own UIControl subclass you can trigger the change as follow:

[self sendActionsForControlEvents:MyPrimaryActionEvent];

This will send all the actions to all the targets registered for MyPrimaryActionEvent event. And you're done.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文