无法成功删除GustureRecognizer iOS

时间:2012-11-24 11:41:56

标签: ios uigesturerecognizer

我的iPad应用程序中有一个按钮,当它按下addGestureRecognizer时。当再次按下时,它应该将它们移除。

我的代码位于if块中,我知道正确的代码块已运行,因为NSLog会打印正确的日志消息。

我正在添加这样的识别器:

[self.view addGestureRecognizer:panRecognizer];
[self.view addGestureRecognizer:pinchRecognizer];
[self.view addGestureRecognizer:rotationRecognizer];
[self.view addGestureRecognizer:tapRecognizer];

并尝试将其删除:

[self.view removeGestureRecognizer:panRecognizer];
[self.view removeGestureRecognizer:pinchRecognizer];
[self.view removeGestureRecognizer:rotationRecognizer];
[self.view removeGestureRecognizer:tapRecognizer]; 

这是我的全部功能: http://www.pastelib.com/show/ZlICyb9Q

它不起作用我无法弄清楚为什么,你们有什么建议吗?

提前感谢所有帮助和建议:)

2 个答案:

答案 0 :(得分:2)

您的GestureReconizers在本地声明为-showMenu:方法。 因此,每次按下按钮,您都会创建新的GestureReconizers,因此您没有尝试删除以前的重新配置器,但是您尝试删除新创建的重新配置器。

更改重新对象对象的范围,您的问题将得到解决。

干杯。

使用代码示例进行编辑:

-(IBAction) showMenu:(id) sender {    
    if([self.view.subviews containsObject:menuView]) {

        NSLog(@"remove gestures!");

        // Dismiss menu
        [menuView removeFromSuperview];

        // Remove gestures
        [self.view removeGestureRecognizer:panRecognizer];
        [self.view removeGestureRecognizer:pinchRecognizer];
        [self.view removeGestureRecognizer:rotationRecognizer];
        [self.view removeGestureRecognizer:tapRecognizer];        
    }else{
        // Create menu        
        NSLog(@"add gestures!");

    panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panDetected:)];

    pinchRecognizer = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchDetected:)];

    rotationRecognizer = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotationDetected:)];

    tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapDetected:)];

     tapRecognizer.numberOfTapsRequired = 2;    

     // Make gestures work simultaneously
     panRecognizer.delegate = self;
     pinchRecognizer.delegate = self;
     rotationRecognizer.delegate = self;

        // Set width and height if empty        
       self.menuController = [[menuController alloc] initWithNibName:@"menuController" bundle:nil];

        if(screenWidth == 0) {

            UIInterfaceOrientation currentOrientation = [[UIApplication sharedApplication] statusBarOrientation];

            if(currentOrientation == 1 || currentOrientation == 2) {
                // portrait
                screenHeight    = self.menuController.view.frame.size.height;
                screenWidth     = self.menuController.view.frame.size.width;
            }else{
                // Landscape
                screenHeight     = self.menuController.view.frame.size.width;
                screenWidth    = self.menuController.view.frame.size.height+20; //height+20px for the status bar
            }
        }

        int menuHeight = 80;
        self.menuController.view.frame = CGRectMake(0,screenHeight-(menuHeight-20), screenWidth, menuHeight);

        self.menuView = self.menuController.view;

        [self.menuController setDelegate:self];

        [self addChildViewController:self.menuController];
        [self.menuController didMoveToParentViewController:self];
        [self.view addSubview:menuView];


        // Add image gestures
        [self.view addGestureRecognizer:panRecognizer];
        [self.view addGestureRecognizer:pinchRecognizer];
        [self.view addGestureRecognizer:rotationRecognizer];
        [self.view addGestureRecognizer:tapRecognizer];

        [self.view bringSubviewToFront:menuButton];        

    }
}

答案 1 :(得分:1)

我在我的项目中做到了这一点并且有效:

while (cellMainView.descripcionArticulo.gestureRecognizers.count) {
    [cellMainView.descripcionArticulo removeGestureRecognizer:[cellMainView.descripcionArticulo.gestureRecognizers objectAtIndex:0]];
}