When touches Cancelled method get invoked in iPhone?

前端 未结 5 741
南笙
南笙 2021-02-05 07:49

I am able to understand that when user just touches the view, touches Began and Ended called. When user swipes their hand on a view, touches Moved method gets called. But when d

相关标签:
5条回答
  • 2021-02-05 08:05

    And, from the Event Handling Guide for iOS, p. 19:

    It sends the touchesCancelled:withEvent: message when the touch sequence is cancelled by a system event, such as an incoming phone call.

    0 讨论(0)
  • 2021-02-05 08:06

    From the Apple Reference documents

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

    Discussion

    This method is invoked when the Cocoa Touch framework receives a system interruption requiring cancellation of the touch event; for this, it generates a UITouch object with a phase of UITouchPhaseCancel. The interruption is something that might cause the application to be no longer active or the view to be removed from the window

    When an object receives a touchesCancelled:withEvent: message it should clean up any state information that was established in its touchesBegan:withEvent: implementation.

    The default implementation of this method does nothing. However immediate UIKit subclasses of UIResponder, particularly UIView, forward the message up the responder chain.

    0 讨论(0)
  • 2021-02-05 08:07

    Note: touches also get cancelled if you start a UIView animation after touchesBegan. To prevent this make sure you include UIViewAnimationOptionAllowUserInteraction:

    e.g.

    [UIView animateWithDuration:0.3 delay:0 options:UIViewAnimationOptionAllowUserInteraction animations:^{
                self.aView.hidden = NO;
                self.aView.alpha = 1;
            } completion:nil];
    
    0 讨论(0)
  • 2021-02-05 08:13

    I think probably the most common reason for touchesCancelled being called (since iOS 3.2 anyway) is following the recognition of a gesture by a UIGestureRecognizer. If your view has any kind of gesture recognizer attached to it then it is often very important to provide a custom implementation of the touchesCancelled method - note this includes ready made views that use gesture recognizers, including UIScrollView.

    By default, gesture recognizers cancel the delivery of touches to the hit-test view upon recognition, although this behaviour can be disabled. This involves sending the touchesCancelled message to that view, most likely following a touchesBegan or touchesMoved message. If your touch handling code is relying on code implemented in the touchesEnded method, it is possible this may never be fired and some kind of serious problem could occur, hence the need to properly tie up any loose ends in touchesCancelled.

    The ins and outs of gesture recognizer functionality is obviously a bit more complex than I've mentioned here - I would thoroughly recommend reading Apple's Gesture Recognizers documentation.

    Also, check out the WWDC videos on gesture recognizers (starting from 2010).

    0 讨论(0)
  • 2021-02-05 08:28

    I was handling touchesBegan()/touchesMoved() on a view under UIScrollView, which is challenging. My touches kept cancelled by somewhere when I pinch (somehow it is OK with single touch movement), I was investigating how to stop being cancelled. I figured out, that there is a property Can Cancel On Scroll on UIScrollView, and you may check it off to stop being cancelled, if your case is similar to my case.

    It sounds there are many cases where your touches are being cancelled, so my answer is just one of them.

    0 讨论(0)
提交回复
热议问题