iOS: Two Gestures, One Target-Action

本秂侑毒 提交于 2019-12-13 05:09:20

问题


I'm implementing the Messages app copy-message feature.

You can either double tap or long press on a message to copy it.

How do I do that?

I was thinking of adding two gesture recognizers to the view, one UITapGestureRecognizer (with numberOfTapsRequired set to 2) and one UILongPressGestureRecognizer. They would both have the same target & action.

Then, I think for each of them, I'd call requireGestureRecognizerToFail:, passing the other gesture recognizer.

Is my thinking correct? Is there anything I'm missing, or is there a better way to do this?


回答1:


Simply add the gestures to your view (easy to do programatically) and set the selector to the desired method. However, you're probably going to get some push back since you don't provide any code or hint that you have tried to solve your problem before coming here. I'm new here as well, but have seen some questions put on hold for those reasons.




回答2:


As you said that for double tap and long press on a message to copy. So both of them are using for same action. So I think you can do it on same method.




回答3:


You can try this method in UIGestureRecognizerDelegate

gestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer:

refer this for more detail: https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIGestureRecognizerDelegate_Protocol/Reference/Reference.html#//apple_ref/occ/intfm/UIGestureRecognizerDelegate/gestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer:

this helps recognize more than one gesture recognizer at a time.




回答4:


Yes, as you say, create two gesture recognizers (one long-press and one double-tap) and add them both to the same view.

Don't call requireGestureRecognizerToFail: on either of them because long-press & double-tap gestures play nice together by default.

You can give them both the same target and action, but each gesture requires different logic to determine whether you should show the copy menu.

- (void)messageCopyMenuShowAction:(UILongPressGestureRecognizer *)gestureRecognizer
{
    BOOL doubleTap = (gestureRecognizer.numberOfTapsRequired == 2);
    if ((doubleTap && gestureRecognizer.state == UIGestureRecognizerStateEnded) ||  // double-tap
        (!doubleTap && gestureRecognizer.state == UIGestureRecognizerStateBegan)) { // long-press
        // Show copy menu.
    }
}


来源:https://stackoverflow.com/questions/19614839/ios-two-gestures-one-target-action

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!