如何删除手势识别器 [英] How to remove gesture recogniser

查看:130
本文介绍了如何删除手势识别器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我正在为叠加视图添加手势识别器。当点击屏幕时,我希望这个叠加层消失。已经说过添加手势识别器会覆盖内部触摸和其他按钮点击事件。我需要这个,因此我需要removegesturerecognizer。我可以使用这种方法,但我有一个问题。我的代码如下 -

SO, I am adding a gesture recogniser to an overlay view. When tapped on screen i want this overlay to go away. Having said that adding a gesture recognizer overrides the "touch up inside" and other button click events. I need this back therefore i need to removegesturerecognizer. I can use this method however i have a problem. My code below -

- (void)helpClicked
{
    CGRect visibleBounds = [self convertRect:[self bounds] toView:viewContainer];
    CGFloat minimumVisibleX = CGRectGetMinX(visibleBounds);
    UIImageView * helpOverlay = [[UIImageView alloc]initWithFrame:CGRectMake(minimumVisibleX, 0, 1024, 768)];
    UIImage * helpImage = [UIImage imageNamed:@"HelpOverLay.png"];
    [helpOverlay setImage:helpImage];
    helpOverlay.tag = 50;
    self.scrollEnabled = NO;
    [self addSubview:helpOverlay]; 
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] 
                               initWithTarget:self
                               action:@selector(dismissView)];

[self addGestureRecognizer:tap];    

}

我在这里拿覆盖其他视图。

Here i am taking the overlay off the other view.

- (void) dismissView
{
    UIView *overlay = [self viewWithTag:50];
    [overlay removeFromSuperview];
    self.scrollEnabled = YES;
}

我的问题是如何在第二种方法中删除手势识别器?我不能将变量tap传递给这个方法,也不能在之前的方法中删除它。有什么指针吗?在涉及到事件时,我遇到了很多传递变量问题。

My question is how do i remove the gesture recognizer in the second method? I cant pass the variable tap into this method nor can i remove it in the previous method either. Any pointers? Ive been stuck with quite a lot of passing variable problems when it comes to events.

推荐答案

来自 WWDC 2015,Cocoa Touch最佳实践,如果您建议保留房产或iVar需要稍后访问它,不要使用 viewWithTag:

From the WWDC 2015, Cocoa Touch Best Practices, it is suggested that you keep a property or iVar if you need to access it later, and don't go with using viewWithTag:.

这样可以避免一些麻烦:

This saves you from some trouble:


  1. 处理多个手势时,您可以直接删除所需的手势并删除它。 (无需迭代所有视图的手势以获取正确的手势)

  2. 当您进行迭代时,通过标记查找正确的手势,如果您有多个标签,则会产生误导在视图上,以及与特定标记冲突时




(即)您是第一次实现它标签,一切按预期工作
。稍后你会处理另一个功能,让我们说
会打破这个并导致你不期望的不良行为。 Log
不会给你一个警告,你可以得到最好的东西,具体情况是崩溃信号无法识别的选择器发送到实例有时你不会得到任何



解决方案



声明iVar

Solution

Declare the iVar

@implementation YourController {
    UITapGestureRecognizer *tap;
}

设置视图

- (void) helpClicked {
    //Your customization code

    //Adding tap gesture
    tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismissView)];
    [self addGestureRecognizer:tap];
}

直接删除手势

- (void) dismissView {
    [self.view removeGestureRecognizer:tap];
}

这篇关于如何删除手势识别器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆