简体   繁体   English

轻按一下手势识别器

[英]Single Tap TapGestureRecognizer

I am trying to use this code for a tap gesture recognizer, it works fine when number of taps required is set to 2, but when I set the number of taps required to 1 it stop functioning. 我正在尝试将此代码用于轻击手势识别器,当所需的轻击次数设置为2时,它工作正常,但是当我将所需的轻击次数设置为1时,它将停止运行。 I appreciate any help in getting this to work. 感谢您为使它正常工作所提供的帮助。

   UITapGestureRecognizer *doubleTap =
    [[UITapGestureRecognizer alloc]
     initWithTarget:self
     action:@selector(tapDetected:)];
   [doubleTap setNumberOfTapsRequired : 1];
[doubleTap setDelaysTouchesBegan : YES];
    [self.view addGestureRecognizer:doubleTap];

You try this:- 您可以尝试以下方法:

UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget: self action:@selector(tapDetected:)];
singleTap.numberOfTapsRequired = 1; 
[self.view addGestureRecognizer:singleTap];

UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc] initWithTarget: self action:@selector(tapDetected:)];
doubleTap.numberOfTapsRequired = 2; 
[self.view addGestureRecognizer:doubleTap];

[singleTap requireGestureRecognizerToFail:doubleTap];

This works for me. 这对我有用。

If You only want the receiver to respond to one/first touch then setDelaysTouchesBegan:NO Since this is used for processing touches in UITouchPhaseBegan so it is analyzed and is prevented from being delivered. 如果只希望接收者对一个/第一次触摸做出响应,则setDelaysTouchesBegan:NO因为这是用于处理UITouchPhaseBegan触摸,所以将对其进行分析并阻止进行传递。 Property discussion from documentation: 文档中的属性讨论:

When the value of this property is NO (the default), views analyze touch events in UITouchPhaseBegan and UITouchPhaseMoved in parallel with the receiver. 当此属性的值为NO(默认值)时,视图将与接收器并行分析UITouchPhaseBegan和UITouchPhaseMoved中的触摸事件。 When the value of the property is YES, the window suspends delivery of touch objects in the UITouchPhaseBegan phase to the view. 当该属性的值为YES时,该窗口将挂起UITouchPhaseBegan阶段中的触摸对象向视图的传递。 If the gesture recognizer subsequently recognizes its gesture, these touch objects are discarded. 如果手势识别器随后识别出其手势,则将这些触摸对象丢弃。 If the gesture recognizer, however, does not recognize its gesture, the window delivers these objects to the view in a touchesBegan:withEvent: message (and possibly a follow-up touchesMoved:withEvent: message to inform it of the touches' current locations). 但是,如果手势识别器无法识别其手势,则窗口会通过touchesBegan:withEvent:消息(可能还有后续touchesMoved:withEvent:消息将这些对象告知触摸的当前位置)将这些对象传递给视图。 。 Set this property to YES to prevent views from processing any touches in the UITouchPhaseBegan phase that may be recognized as part of this gesture. 将此属性设置为YES,可以防止视图处理UITouchPhaseBegan阶段中可能被识别为该手势的任何触摸。

It looks to me like you are simply failing to set the delegate of your GestureRecognizer. 在我看来,您只是无法设置GestureRecognizer的委托。 Are you sure the double tap was firing successfully? 您确定双击成功触发了吗? When I placed your code into my project I see the same behavior but setting the delegate properly and using shouldRecognizeSimultaneouslyWithGestureRecognizer causes it to recognize the single tap properly. 当我将代码放入项目时,我会看到相同的行为,但是正确设置委托并使用shouldRecognizeSimultaneouslyWithGestureRecognizer会使其正确识别单击。

UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self
                                                                            action:@selector(tapDetected:)];
[doubleTap setDelegate:self];
[doubleTap setNumberOfTapsRequired : 1];
[doubleTap setDelaysTouchesBegan : YES];
[picker addGestureRecognizer:doubleTap];

-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
    if ([gestureRecognizer isKindOfClass:UITapGestureRecognizer.class] &&
        [otherGestureRecognizer isKindOfClass:UITapGestureRecognizer.class])
    {
        return YES;
    }
    else
    {
        return NO;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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