【问题标题】:How Do I Log UIControlEvents?如何记录 UIControlEvents?
【发布时间】:2014-02-14 16:57:24
【问题描述】:

我正在尝试弄清楚如何记录 UIControlEvents,以便我可以准确地看到要添加到 UIButton 中的事件。

我似乎无法弄清楚。到目前为止,这是我尝试过的:

[button addTarget:self action:@selector(buttonTouched:forEvent:) forControlEvents:UIControlEventAllEvents];


-(void)buttonTouched:(UIButton *)button forEvent:(UIControlEvents)event{

NSLog(@"%s", __PRETTY_FUNCTION__);

switch (event) {
    case UIControlEventTouchDown:
        NSLog(@"UIControlEventTouchDown");
        break;
    case UIControlEventTouchDownRepeat:
        NSLog(@"UIControlEventTouchDownRepeat");
        break;
    case UIControlEventTouchDragInside:
        NSLog(@"UIControlEventTouchDragInside");
        break;
    case UIControlEventTouchDragOutside:
        NSLog(@"UIControlEventTouchDragOutside");
        break;
    case UIControlEventTouchDragEnter:
        NSLog(@"UIControlEventTouchDragEnter");
        break;
    case UIControlEventTouchDragExit:
        NSLog(@"UIControlEventTouchDragExit");
        break;
    case UIControlEventTouchUpInside:
        NSLog(@"UIControlEventTouchUpInside");
        break;
    case UIControlEventTouchUpOutside:
        NSLog(@"UIControlEventTouchUpOutside");
        break;
    case UIControlEventTouchCancel:
        NSLog(@"UIControlEventTouchCancel");
        break;
    case UIControlEventValueChanged:
        NSLog(@"UIControlEventValueChanged");
    default:
        break;
}
}

唯一打印的日志是__PRETTY_FUNCTION__ 日志。我也试过这个,以防 switch 语句是问题:

if (event == UIControlEventTouchDown) {
    NSLog(@"UIControlEventTouchDown");
}
if (event == UIControlEventTouchDownRepeat){
    NSLog(@"UIControlEventTouchDownRepeat");
}
etc...

仍然没有任何日志。我做错了什么?

编辑:我通过更改添加到按钮的事件进行了进一步实验,如下所示:

[button addTarget:self action:@selector(buttonTouched:forEvent:) forControlEvents:UIControlEventTouchDown];

然后我非常具体地记录了输入:

-(void)buttonTouched:(UIButton *)button forEvent:(UIControlEvents)event{

    NSLog(@"UIControlEventTouchDown = %i", UIControlEventTouchDown);
    NSLog(@"event = %i", event);
}

这是输出:

UIControlEventTouchDown = 1
event = 350486320

有人知道发生了什么吗?

编辑 2:感谢 TAKEanice,我现在意识到我没有正确编写选择器。我不确定打印出的事件日志到底是什么,但是虽然我基本上将UIEvent 转换为枚举类型(UIControlEvents),但它仍然在日志中给出了一个值。 无论如何,在我看来,仍然必须有办法找出 UIControlEvents 是什么,但我无法从文档中看到。 但是,Apple 在 UIControl 文档中提到了它:

子类化注释 您可能出于以下两个原因想要扩展 UIControl 子类:

观察或修改动作消息到目标的调度 特定事件为此,请覆盖 sendAction:to:forEvent:, 评估传入的选择器、目标对象或 UIControlEvents 位 掩码,然后按要求进行。

提供自定义跟踪行为(例如,更改 突出显示外观)为此,请覆盖以下一项或全部 方法:beginTrackingWithTouch:withEvent:, continueTrackingWithTouch:withEvent:, endTrackingWithTouch:withEvent:

当为sendAction:to:forEvent: 传入的参数是UIEvent 时,如何评估UIControlEvents 位掩码? 有什么想法吗?

【问题讨论】:

  • 如果在 switch 语句的开头放置断点,那么 event 是否有值?
  • @WyattPage 问题不在于事件没有价值。问题是该值不对应于任何 UIControlEventTouch ......等。我不明白这是为什么。
  • @daveMac 检查我的更新答案

标签: ios objective-c event-handling nslog uicontrolevents


【解决方案1】:

试试这个:

// somewhere
[self.btn addTarget:self action:@selector(buttonTouched:forEvent:) forControlEvents:UIControlEventAllEvents];


- (void)buttonTouched:(UIButton*)sender forEvent:(id)event {
    NSSet *touches = [event allTouches];
    UITouch *touch = [touches anyObject];
    UITouchPhase phase = touch.phase;
}

更新:
你会得到接触,然后在最后一次接触中你会有相位,这表明这是什么事件

解释:

根据触摸的位置和相位,您将能够计算出事件:

  • UITouchPhaseEnded + 触摸位置在范围内 => UITouchUpInside
  • UITouchPhaseEnded + 触摸位置超出范围 => UITouchUpOutside
  • ...

【讨论】:

  • 这是一个不同实现的选项,但它并没有真正回答这个问题。此外,UITouchPhase 的值非常模糊。
【解决方案2】:

您在事件参数中收到的不是“UIControlEvent”而是“UIEvent”,这是一个完全不同的类。

文档说: UIEvent 属于某种类型。这些可以是任何 UIEventType 枚举,它们是

  • UIEventTypeTouches
  • UIEventTypeMotion
  • UIEventTypeRemoteControl

事件也可以有一个子类型,这些子类型似乎只与 remoteControl 类型有关。

https://developer.apple.com/library/ios/documentation/uikit/reference/UIEvent_Class/Reference/Reference.html

因此,您不会在事件参数中找到确切的 ControlEventType,而只能找到控件响应的“一般”操作类型。

似乎你唯一的选择是为不同的 UIControlEvents 触发不同的方法。

----编辑----
另一种选择是自己实现 UIControl,这样您就可以准确地计算出用户何时以及用多少手指等触摸的位置。

【讨论】:

  • 也许我不清楚。您让按钮调用任何操作的方法不应该是-(void)buttonTouched:(UIButton *)button forEvent:(UIControlEvents)event,而是-(void)buttonTouched:(UIButton *)button forEvent:(UIEvent *)event
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-02-12
  • 2017-07-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多