4

我试图弄清楚如何记录 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,我现在意识到我没有正确编写选择器。我不确定打印出的事件日志到底是什么,但是虽然我基本上将 aUIEvent转换为枚举类型 ( UIControlEvents),但它仍然在日志中给出了一个值。无论如何,在我看来,仍然必须有办法找出 UIControlEvents 是什么,但我无法从文档中看到。但是,Apple 在 UIControl 文档中提到了它:

子类化说明 您可能出于以下两个原因之一想要扩展 UIControl 子类:

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

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

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

4

2 回答 2

2

Try this out:

// 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;
}

UPDATE:
you will get touches, then in the last touch you will have phase, which indicates what is this event

EXPLANATION:

From the position of the touch and the phase you will be able to compute the event:

  • UITouchPhaseEnded + touch location is within bounds => UITouchUpInside
  • UITouchPhaseEnded + touch location is outside bounds => UITouchUpOutside
  • ...
于 2014-02-14T18:29:23.270 回答
1

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

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

  • UIEventTypeTouches
  • UIEventTypeMotion
  • UIEventTypeRemoteControl

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

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

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

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

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

于 2014-02-14T18:18:01.433 回答