Touches Ended not being called

前端 未结 7 1076
無奈伤痛
無奈伤痛 2021-01-17 09:53

I\'ve playing around with recognizing the touches in an iOS Application, and I have this simple code

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent         


        
相关标签:
7条回答
  • 2021-01-17 10:16

    I had the same problem and solved it with this simple code in the touches ended:

    NSInteger touchCount = 0;
    for (UITouch *touch in touches) {
       touchCount++;
        if([[event allTouches]count] == touchCount){
           // do what you have to do here
        }
    }
    

    // You will get a warning here but don't care about it

    I hope this help!

    0 讨论(0)
  • 2021-01-17 10:20
    • Tap gestures (simple, long, 2xshort, 2xfingers tap) do not allow the calls Touches Ended / Canceled if cancelsTouchesInView == NO
    • They allow its calling ONLY IF cancelsTouchesInView property is the default YES
    • Pinch gesture allows all the calls (to Touches Begin / Moved / Ended / Canceled) ONLY IF cancelsTouchesInView property is set to NO
    • If for the pinch gesture the cancelsTouchesInView property would have been YES, touchesMoved would not have been called

    Inside info:

    I am using:

    • 1 finger UITapGestureRecognizer
    • 1 finger UILongPressGestureRecognizer
    • 1 finger double tap UITapGestureRecognizer
    • 2 fingers tap UITapGestureRecognizer
    • 1 UIPinchGestureRecognizer

    And also should receive one finger panning. But what i have found out is that the Pinch gesture was not Ending even if the user had risen one finger. Somehow iOS is still calling the Pinch's selector with "Changed" state.

    My solution was to use the touchesBegin/Moved/Ended to catch the pan and use the number of active touches as safety check in case the pinch selector would be called.

    Other settings i had to use in making the above configuration:

    self.multipleTouchEnabled = YES;
    [oneFingerTapRecognizer requireGestureRecognizerToFail:oneFingerLongPressRecognizer];
    [oneFingerTapRecognizer requireGestureRecognizerToFail:oneFingerDoubleTapRecognizer];
    [oneFingerLongPressRecognizer requireGestureRecognizerToFail:oneFingerDoubleTapRecognizer];
    [twoFingersTapRecognizer requireGestureRecognizerToFail:twoFingersPinch];
    
    0 讨论(0)
  • 2021-01-17 10:26

    Try overriding the UIView's hitTest method:

    - (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
       return self;
    }
    

    It is possible that when you lift fingers they are not recognised as being in the UIView, sotouchesEnded is not called.

    0 讨论(0)
  • 2021-01-17 10:33

    Try removing any Gesture Recognizers you have on that view. They can interfere with touchesEnded.

    0 讨论(0)
  • 2021-01-17 10:35

    You'd have to set recognizer.cancelsTouchesInView = false in Swift or recognizer.cancelsTouchesInView = NO; in Objective-C

    0 讨论(0)
  • 2021-01-17 10:37

    You have to override all touch methods if you do not call super's implementation. So you have to also implement the touchesMoved:withEvent: and touchesCancelled:withEvent: methods. Implementations can be empty but you have to do it.

    touchesBegan:withEvent:

    If you override this method without calling super (a common use pattern), you must also override the other methods for handling touch events, if only as stub (empty) implementations.

    Based on UIResponder Class Reference

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