【问题标题】:How to distinguish between fired events for a UIButton callback action如何区分 UIButton 回调操作的触发事件
【发布时间】:2013-10-05 06:49:47
【问题描述】:

在为 UIButton 定义回调时,我为同一个操作列出了多个事件

在目标中我希望能够区分是什么事件触发了回调

[button addTarget:self action:@selector(callback:) forControlEvents:UIControlEventTouchDown | UIControlEventTouchCancel];

-(void)callback:(UIButton *)button
{
  // need to be able to distinguish between the events
if (event == canceled)
{
}
if (event == touchDown)
{
}
... etc
}

【问题讨论】:

    标签: ios objective-c uibutton uicontrol uicontrolevents


    【解决方案1】:

    您可以更改您的操作以获取事件参数,如下所示:

    [button addTarget:self action:@selector(callback:event:) forControlEvents:UIControlEventTouchDown | UIControlEventTouchCancel];
    
    -(void)callback:(UIButton *)button (UIEvent*)event {
        ...
    }
    

    在回调中添加第二个参数将使 Cocoa 将事件传递给您,以便您检查触发回调的原因。

    编辑:不幸的是,cocoa does not send you a UIControlEvent,所以要弄清楚是什么控制事件导致了回调并不像检查事件类型那么简单。 UIEvent 为您提供了一系列触摸,您可以对其进行分析以查看它是否是 UITouchPhaseCancelled 触摸。不过,这可能不是最方便的处理方式,因此设置多个回调以将正确的类型传递给您可能会更好:

    [button addTarget:self action:@selector(callbackDown:) forControlEvents:UIControlEventTouchDown];
    [button addTarget:self action:@selector(callbackCancel:) forControlEvents:UIControlEventTouchCancel];
    
    -(void)callbackDown:(UIButton*) btn {
        [self callback:btn event:UIControlEventTouchDown];
    }
    -(void)callbackCancel:(UIButton*) btn {
        [self callback:btn event:UIControlEventTouchCancel];
    }
    -(void)callback:(UIButton*)btn event:(UIControlEvent) event {
        // Your actual callback
    }
    

    【讨论】:

    • 看起来是最简洁的方式。谢谢
    • 如何从 UIEvent 中获取 UIControlEvent。文档没有。
    • 问题是如果你为同一个动作屏蔽多个事件
    【解决方案2】:

    最好做到以下几点:

    [button addTarget:self action:@selector(callback1) forControlEvents:UIControlEventTouchDown];
    [button addTarget:self action:@selector(callback2) forControlEvents:UIControlEventTouchCancel];
    

    当然还有:

    -(void)callback1:(UIButton *)button
    {
    }
    
    -(void)callback2:(UIButton *)button
    {
    }
    

    【讨论】:

    • 根据他在 if 语句中所做的工作量,这可能会导致严重的代码冗余 - 但它当然会起作用。
    • 是的,这就是我更新答案的原因,他需要添加更多参数或做一些额外的编码;)
    【解决方案3】:

    您可以从了解控制事件的第三/第四个方法调用回调:

    - (void)buttonTouchDown:(UIButton*)button {
        [self callback:(UIButton*)button forControlEvent:UIControlEventTouchDown];
    }
    
    
    - (void)buttonTouchCancel:(UIButton*)button {
        [self callback:(UIButton*)button forControlEvent:UIControlEventTouchCancel];
    }
    
    -(void)callback:(UIButton *)button forControlEvent:(UIControlEvents)controlEvents {
        if (controlEvents == UIControlEventTouchDown) {
           <#do something#>
        }
        if (controlEvents == UIControlEventTouchCancel) {
           <#do something#>
        }
    }
    

    【讨论】:

      【解决方案4】:

      如果您需要为每个事件提供不同的行为,您应该考虑为每个事件编写不同的回调。

      【讨论】:

        猜你喜欢
        • 2021-06-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-07-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-05-23
        相关资源
        最近更新 更多