1

当用户在我的应用程序中点击并按住 UIButton 时,会触发 UIControlEventTouchCancel 事件。UIControl 的 Apple 文档没有关于此事件的任何详细说明。为什么会触发?这有什么用途?

[button addTarget:key action:@selector(keyUp:) forControlEvents:UIControlEventTouchUpInside];
[button addTarget:key action:@selector(keyUp:) forControlEvents:UIControlEventTouchUpOutside];
[button addTarget:key action:@selector(keyCancel:) forControlEvents:UIControlEventTouchCancel];
[button addTarget:key action:@selector(keyDown:) forControlEvents:UIControlEventTouchDown];
4

2 回答 2

3

根据文档UIControlEventTouchCancel事件是由于:

取消控件当前触摸的系统事件。

因此,触摸结束并不是由于用户输入,而是由于系统事件(例如内存不足警告或应用程序进入后台)。我不知道为什么您只需按住按钮即可触发它。

我很确定这是与UIResponder'事件类似的(如果不是由 引起的)touchesCancelled:withEvent:事件。这方面的文档稍微详细一些:

该方法在Cocoa Touch框架接收到需要取消触摸事件的系统中断时调用;为此,它会生成一个具有 UITouchPhaseCancel 阶段的 UITouch 对象。中断可能会导致应用程序不再处于活动状态或视图从窗口中删除

于 2016-01-21T18:19:20.740 回答
0

一种情况是屏幕旋转。如果您在按住按钮的同时旋转屏幕,您将收到一个触摸取消事件。

于 2016-10-20T01:36:20.223 回答