【问题标题】:UIGestureRecognizers interfering with UIToolbar?UIGestureRecognizers 干扰 UIToolbar?
【发布时间】:2011-12-05 00:14:00
【问题描述】:

我正在开发一个开源杂志引擎,您可以在 GitHub 上查看它:

https://github.com/interactivenyc/Defrag

我已经在一个名为 MenuPanel 的 UIView 中设置了一个 UIToolbar。由于某种原因,UIToolbar 中的 UIBarButtonItems 没有正确调用它们的操作。这是我用于按钮的语法:

UIBarButtonItem *homeItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"home.png"] style:UIBarButtonItemStylePlain target:self action:@selector(buttonClicked:)];

发生的 是,我在屏幕上单击的任何位置,都会调用在我的主 UIViewController 中声明的 UITapGestureRecognizer。这可以在我的主 UIViewController 的这段代码中进行设置:

- (void)setupGestureRecognizers {

//NSLog(@"setupGestureRecognizer NEW");

UISwipeGestureRecognizer *swipeRecognizer;

swipeRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleGesture:)];
swipeRecognizer.direction = UISwipeGestureRecognizerDirectionRight;
[self.view addGestureRecognizer:swipeRecognizer];
[swipeRecognizer release];

swipeRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleGesture:)];
swipeRecognizer.direction = UISwipeGestureRecognizerDirectionLeft;
[self.view addGestureRecognizer:swipeRecognizer];
[swipeRecognizer release];

swipeRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleGesture:)];
swipeRecognizer.direction = UISwipeGestureRecognizerDirectionUp;
[self.view addGestureRecognizer:swipeRecognizer];
[swipeRecognizer release];

swipeRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleGesture:)];
swipeRecognizer.direction = UISwipeGestureRecognizerDirectionDown;
[self.view addGestureRecognizer:swipeRecognizer];
[swipeRecognizer release];

UITapGestureRecognizer *tapRecognizer;

tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTap:)];
[self.view addGestureRecognizer:tapRecognizer];
[tapRecognizer release];
}

我确信我在尝试执行此操作时在概念上存在一些非常基本的错误。谁能告诉我如何解决这个问题?

作为参考,你可以在这里查看我的主要 DefragViewController:UIViewController:

https://gist.github.com/1431722

还有我的 MenuPanel:这里的 UIView:

gist.github.com/1431728

【问题讨论】:

    标签: ios open-source


    【解决方案1】:

    我解决了我自己的问题。

    我不得不告诉我的 UIViewController 忽略来自任何 UIToolbar 的触摸,如下所示:

    - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch{
    
        //ignore any touches from a UIToolbar
    
        if ([touch.view.superview isKindOfClass:[UIToolbar class]]) {
            return NO;
        }
    
        return YES;
    }
    

    【讨论】:

    • 欢迎来到 Stack Overflow!请务必将您的答案标记为已接受,以便其他人知道有解决方案。
    猜你喜欢
    • 1970-01-01
    • 2015-02-09
    • 2012-07-17
    • 2019-06-17
    • 2015-02-12
    • 2011-04-02
    • 2020-04-01
    • 2017-08-27
    • 2012-11-02
    相关资源
    最近更新 更多