What really happens when call setCancelsTouchesInView?

后端 未结 2 1289
猫巷女王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: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).

提交回复
热议问题