What really happens when call setCancelsTouchesInView?

后端 未结 2 1278
猫巷女王i
猫巷女王i 2020-12-29 11:41

Wondering what really happens when i call setCancelsTouchesInView. It is not covered in the official document http://developer.apple.com/library/ios/#documentation/uikit/ref

相关标签:
2条回答
  • 2020-12-29 12:10

    From the apple developer portal link:

    cancelsTouchesInView — If a gesture recognizer recognizes its gesture, it unbinds the remaining touches of that gesture from their view (so the window won’t deliver them). The window cancels the previously delivered touches with a (touchesCancelled:withEvent:) message. If a gesture recognizer doesn’t recognize its gesture, the view receives all touches in the multi-touch sequence.

    cancelsTouchesInView:

    A Boolean value affecting whether touches are delivered to a view when a gesture is recognized.

    @property(nonatomic) BOOL cancelsTouchesInView

    Discussion

    When this property is YES (the default) and the receiver recognizes its gesture, the touches of that gesture that are pending are not delivered to the view and previously delivered touches are cancelled through a touchesCancelled:withEvent: message sent to the view. If a gesture recognizer doesn’t recognize its gesture or if the value of this property is NO, the view receives all touches in the multi-touch sequence.

    0 讨论(0)
  • 2020-12-29 12:12

    ACB quoted the UIGestureRecognizer reference. To make it a little more concrete, suppose you have a view with a pan gesture recognizer attached, and you have these methods in your view controller:

    - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
        NSLog(@"touchesBegan");
    }
    
    - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
        NSLog(@"touchesMoved");
    }
    
    - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
        NSLog(@"touchesEnded");
    }
    
    - (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
        NSLog(@"touchesCancelled");
    }
    
    - (IBAction)panGestureRecognizerDidUpdate:(UIPanGestureRecognizer *)sender {
        NSLog(@"panGesture");
    }
    

    And of course the pan gesture recognizer is configured to send the panGestureRecognizerDidUpdate: message.

    Now suppose you touch the view, move your finger enough for the pan gesture to be recognized, and then lift your finger. What does the app print?

    If the gesture recognizer has cancelsTouchesInView set to YES, the app will log these messages:

    touchesBegan
    touchesMoved
    touchesCancelled
    panGesture
    panGesture
    (etc.)
    

    You might get more than one touchesMoved before the cancel.

    So, if you set cancelsTouchesInView to YES (the default), the system will cancel the touch before it sends the first message from the gesture recognizer, and you won't get any more touch-related messages for that touch.

    If the gesture recognizer has cancelsTouchesInView set to NO, the app will log these messages:

    touchesBegan
    touchesMoved
    panGesture
    touchesMoved
    panGesture
    touchesMoved
    panGesture
    (etc.)
    panGesture
    touchesEnded
    

    So, if you set cancelsTouchesInView to NO, the system will continue sending touch-related messages for the gesture touch, interleaved with the gesture recognizer's messages. The touch will end normally instead of being cancelled (unless the system cancels the touch for some other reason, like the home button being pressed during the touch).

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