31i45
31i45

Reputation: 315

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

I'm trying to write a program using UIBezierPath and touches Methods. I am confused by two touches method:

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

I don't understand when these method are called or how can I call them. At first I thought they both were the same, until I read somewhere that they weren't. Question is: How can I call one of these methods and how are they different from one another?

Upvotes: 1

Views: 838

Answers (2)

Rafał Augustyniak
Rafał Augustyniak

Reputation: 2031

You shouldn't call these methods. They are called by Cocoa framework for you. You just need to implement them to provide correct (the native one) behaviour o your subclass of UIResponser or your custom UIGestureRecognizer subclass.

Please refer to Apple's guide about subclassing UIResponder for more details about how you should implement these methods.

From Apple's documentation about - (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event method:

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

From Apple's documentation about - (void)touchesCancelled:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event method:

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

Upvotes: 3

Ermiar
Ermiar

Reputation: 1077

This document from Apple give you an answer regarding the touchesCancelled event :

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.

These method are necessary when you implement custom touch event handling in your application for a custom object. It allow you to clean up resources when the user stop touching your custom object or when the touches is cancelled.

Upvotes: 1

Related Questions