简体   繁体   中英

iOS TapGestureRecognizer — Multiple Touches Implementation

GOAL: I like to detect how many fingers the user used to tap the screen.

Possible Solution: Use a UITapGestureRecognizer . I'd like to just use one recognizer and check the number of touches with recognizer.numberOfTouches inside the method it calls. However, it numberOfTouchesRequired seems to be a strictly equal qualifier rather than a greater than or equal to qualifier. ie If I set numberOfTouchesRequired to 1, my UITapGestureRecognizer doesn't respond to 2-finger taps. Therefore, I must create 5 different UITapGestureRecognizers , each with a different numberOfTouchesRequired (1, 2, 3, 4, and 5).

Is this this optimal solution? It feels hacky to me.

You could try subclassing UIGestureRecognizer and creating a custom recognizer that detects how many fingers are on the screen. Here's notes on how to subclass UIGestureRecognizer: https://developer.apple.com/library/ios/documentation/uikit/reference/UIGestureRecognizer_Class/Reference/Reference.html#//apple_ref/doc/uid/TP40009279-CH1-SW2

Or you could implement the touchesBegan: withEvent method for a view and count the number of touches.

-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    int num_touches = [touches count];
}

Just make sure that

view.multipleTouchEnabled = YES;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM