gpt4 book ai didi

ios - 如何区分 UIButton 回调操作的触发事件

转载 作者:可可西里 更新时间:2023-11-01 06:19:41 26 4
gpt4 key购买 nike

在为 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
}

最佳答案

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

[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
}

关于ios - 如何区分 UIButton 回调操作的触发事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19076741/

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