禁用手势识别器

Posted

技术标签:

【中文标题】禁用手势识别器【英文标题】:Disable gesture recognizer 【发布时间】:2011-05-13 11:33:56 【问题描述】:

我有两种类型的识别器,一种用于点击,一种用于滑动

UIGestureRecognizer *recognizer;

//TAP
recognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(numTap1:)];
[(UITapGestureRecognizer *)recognizer setNumberOfTouchesRequired:1];
[self.view addGestureRecognizer:recognizer];
self.tapRecognizer = (UITapGestureRecognizer *)recognizer;
recognizer.delegate = self;
[recognizer release];

//SWIPE RIGHT
recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeRight:)];
self.swipeRightRecognizer =(UISwipeGestureRecognizer *)recognizer;
swipeRightRecognizer.direction = UISwipeGestureRecognizerDirectionRight;
[self.view addGestureRecognizer:swipeRightRecognizer];
self.swipeRightRecognizer = (UISwipeGestureRecognizer *)recognizer;
[recognizer release];

使用此功能,我可以禁用对某些对象的点击。

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch 

if ((touch.view == loseView) || (touch.view == subBgView) || (touch.view == btnAgain)) 

    return NO;


return YES;

如何禁用滑动?

非常感谢!

【问题讨论】:

【参考方案1】:

UIGestureRecognizer 有一个名为enabled 的属性。这应该足以禁用您的滑动:

swipeGestureRecognizer.enabled = NO;

编辑:对于 Swift 5

swipeGestureRecognizer.isEnabled = false

【讨论】:

@PeyloW:+1 我想我需要将其删除并重新添加到视图中。 另外,您可以在手势附加到的视图上设置 userInteractionEnabled = NO。如果您正在使用某种叠加层,但无论如何您都希望禁用底层内容,这将非常方便。这也将根据视图的隐藏值和 alpha 值自动切换。【参考方案2】:

为什么不也为滑动手势识别器设置委托,并在同一个委托方法中处理它们。

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch 
    if ( [gestureRecognizer isMemberOfClass:[UITapGestureRecognizer class]] ) 
        // Return NO for views that don't support Taps
     else if ( [gestureRecognizer isMemberOfClass:[UISwipeGestureRecognizer class]] ) 
        // Return NO for views that don't support Swipes
    

    return YES;

【讨论】:

【参考方案3】:

我也有类似的问题。我的一些禁用用户同时点击和滑动,因此应用程序移动到下一个屏幕。我设置了一个选项,允许他们使用三指轻敲。我需要在应用程序首次启动时调用 popoverControllerDidDismissPopover 委托选项。所以我写了一个结合上面答案的方法。它会查找所有滑动手势识别器并将其关闭,然后打开我的点击手势识别器。

- (void)changeGestureRecognizer 
    // Three finger tap to move to next screen
    if ([Globals sharedInstance].useDoubleTapToMoveToNextScreen) 

        // Let’s see which gestures are active and turn off the swipes
        for (UIGestureRecognizer *gestureRecognizer in self.view.gestureRecognizers) 
            NSLog(@"The gestureRecognizer is %@", gestureRecognizer);
            if ( [gestureRecognizer isMemberOfClass:[UISwipeGestureRecognizer class]] ) gestureRecognizer.enabled = NO;
        
        // Add the three finger tap
        UITapGestureRecognizer *twoFingerTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeNext)];
        [twoFingerTap setNumberOfTapsRequired:1];
        [twoFingerTap setNumberOfTouchesRequired:3];
        [self.view addGestureRecognizer:twoFingerTap];

    

【讨论】:

以上是关于禁用手势识别器的主要内容,如果未能解决你的问题,请参考以下文章

仅针对特定视图禁用手势识别器

禁用手势识别器

在 AVPlayerViewController 中禁用手势识别器

禁用和启用页面视图控制器手势识别器?

禁用手势识别器

在玩游戏期间禁用 iOS 多任务手势识别器