具有多个操作的UIButton:如何防止其他操作触发 [英] UIButton with multiple actions: How to prevent other actions from firing

查看:148
本文介绍了具有多个操作的UIButton:如何防止其他操作触发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图找到一种消费按钮的方法,而不是从响应链本身,而是从绑定到该按钮的其他操作方法。我已经查看了这个解决方案,并找不到它。

I'm trying to find a way to consume a button press, not from the response chain per se, but from other action methods bound to that button. I've looked all over for this solution and have been unable to find it.

例如,让我说一个按钮与一个事件的选择器: / p>

For example, lets say I set up a button with a selector for an event:

[button addTarget:self action:@selector(handler1:) forControlEvents:UIControlEventTouchUpInside];

然后在代码中,基于特定的应用环境我想为同一个控制事件到同一按钮:

Then later in the code, based on specific app circumstances I want to add another event handler for the same control event to the same button:

[button addTarget:self action:@selector(handler2:) forControlEvents:UIControlEventTouchUpInside];

这很好,这两个事件确实被调用。但是我的问题是,没有从按钮中删除handler1,我怎么能这样,使得当handler2被调用时,事件是被消耗和handler1不被调用?

This works fine, both events are indeed called. But my question is, without removing handler1 from the button, how can I make it so that when handler2 is called, the event is "consumed" and handler1 does not get called?

我有这样的情况的原因是,我想让我的应用程序进入教程模式,其中我在教程模式下动态绑定新事件到按钮。本教程将指导用户点击某个按钮,但我想要忽略屏幕上其他按钮上的轻击事件,基本上强迫用户点击请求的按钮继续教程。因此,当用户输入教程时,每个按钮都会添加一个新的TouchUpInside处理程序。我想要这个新的处理程序被首先调用,并阻止原来的处理程序执行。

The reason I have such a circumstance is that I want my app to go into a tutorial mode, where I dynamically bind new events to buttons while in the tutorial mode. The tutorial will instruct the user to tap a certain button, but I want the tap events on the other buttons on the screen to be ignored, basically forcing the user to tap the requested button to continue with the tutorial. So every button gets a new TouchUpInside handler added when the user enters the tutorial. I want this new handler to be called first and block the original handler from executing.

我已经能够实现它被调用,首先通过在 NSSet 中的所有原始事件,然后调用 [button removeTarget ...] 用于所有现有事件。然后我添加我的动态事件,然后从集合中重新添加所有原始事件。

I have been able to achieve it being called first by getting all the original events in an NSSet, then calling [button removeTarget...] for all the existing events. Then I add my dynamic event and then re-add all the original events from the set. This works in the debugger to show that my dynamic event is indeed called first.


  • 例如:

  • handler1会在按下时执行某些操作(按钮的默认处理程序)

  • handler2动态添加,并与教程控制器通信,消耗tap事件。

当不在教程模式时,我想handler1仍然做它应该做的,但如果handler2存在我想要的方法运行,然后阻止handler1被调用。我不能失去handler1从按钮,因为当教程结束,我想让应用程序按预期工作。此外,我可能有某些情况下,我仍然希望handler1被调用。

When not in tutorial mode, I want handler1 to still do what it's supposed to do, but if handler2 exists I want that method to run, then prevent handler1 from being called. I can't lose handler1 from the button because when the tutorial ends, I'd want the app to work as intended. In addition, I may have certain cases where I still want handler1 to be called.

那么,是否可以消费一个事件并保持其他相关事件不被触发?

So, is it possible to consume an event and keep other related events from firing?

在handler2中做一个 [button resignFirstResponder] ,但似乎不工作。它仍然调用原始按钮事件处理程序。

I have tried doing a [button resignFirstResponder] in handler2, but that doesn't seem to work. It still calls the original button event handler.

推荐答案

基于 @ danielquokka 关于覆盖方法 sendActionsForControlEvents:的想法,我子类化UIButton并添加以下代码。它工作正常。发射事件后,它将阻止UI事件0.5秒。

Based on the @danielquokka idea about override the method sendActionsForControlEvents:, I subclass UIButton and add following codes. It works fine. After firing event, it will block UI events for 0.5 seconds.

- (void)sendAction:(SEL)action to:(id)target forEvent:(UIEvent *)event
{
    [super sendAction:action to:target forEvent:event];
    self.userInteractionEnabled = NO;

    ///! After handle UIEvent, block this button UI events for a while.
    [self performSelector:@selector(delayEnable) withObject:nil afterDelay:0.5];
}

- (void)delayEnable
{
    self.userInteractionEnabled = YES;
}



UPDATE



另一种忽略UI事件的方法是通过 [[UIApplication sharedApplication] beginIgnoringInteractionEvents] [[UIApplication sharedApplication] endIgnoringInteractionEvents]

beginIgnoringInteractionEvents endIgnoringInteractionEvents 应用程序,直到 endIgnoringInteractionEvents 被调用。

beginIgnoringInteractionEvents and endIgnoringInteractionEvents will block all touch events for the Application until endIgnoringInteractionEvents called.

这篇关于具有多个操作的UIButton:如何防止其他操作触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆