我试图弄清楚如何记录 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