0

我的 iPad 应用程序中有一个按钮,当按下它时,它会添加 GestureRecognizer。再次按下时,它应该删除它们。

我的代码在 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

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

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

4

2 回答 2

2

您的 GestureReconizers 在本地声明到 -showMenu: 方法。因此,每次按下按钮时,您都会创建新的 GestureReconizer,因此您不会尝试删除以前的 reconizer,而是尝试删除新创建的。

改变你的 reconizers 对象的范围,你的问题就会得到解决。

干杯。

使用代码示例编辑:

-(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];        

    }
}
于 2012-11-24T13:42:31.987 回答
1

我在我的项目中这样做了,它奏效了:

while (cellMainView.descripcionArticulo.gestureRecognizers.count) {
    [cellMainView.descripcionArticulo removeGestureRecognizer:[cellMainView.descripcionArticulo.gestureRecognizers objectAtIndex:0]];
}
于 2015-03-23T16:21:00.310 回答