iOS touches 方法 (Cancelled & Ended) 何时调用?

When does a iOS touches method (Cancelled & Ended) called?

我正在尝试使用 UIBezierPath 和 touches 方法编写程序。 我对两次触摸方法感到困惑:

- (void)touchesCancelled:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event;
- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event;

我不明白这些方法是什么时候调用的,或者我该如何调用它们。起初我以为他们都是一样的,直到我在某处读到他们不是。问题是:如何调用其中一种方法,它们之间有何不同?

This document 来自 Apple 给你一个关于 touchesCancelled 事件的答案:

If a responder creates persistent objects while handling events, it should implement the touchesCancelled:withEvent: method to dispose of those objects if the system cancels the sequence. Cancellation occurs when an external event—for example, an incoming phone call—disrupts the current app’s event processing. Note that a responder object should also dispose of any persistent objects when it receives the last touchesEnded:withEvent: message for a multitouch sequence.

当您在应用程序中为自定义对象实现自定义触摸事件处理时,这些方法是必需的。 它允许您在用户停止触摸您的自定义对象或触摸被取消时清理资源。

您不应该调用这些方法。它们由 Cocoa 框架为您调用。您只需要实现它们以在您的 UIResponser 子类或您的自定义 UIGestureRecognizer 子类中提供正确的(原生的)行为。

请参阅 Apple's guide 关于子类化 UIResponder 以了解有关如何实现这些方法的更多详细信息。

来自Apple's documentation关于- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event方法:

Tells the responder when one or more fingers are raised from a view or window.

来自Apple's documentation关于- (void)touchesCancelled:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event方法:

Sent to the receiver when a system event (such as a low-memory warning) cancels a touch event.