2

我有一个UIView我添加了一个UITapGestureRecognizer

UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapDetected:)];
tapRecognizer.numberOfTapsRequired=1;
tapRecognizer.numberOfTouchesRequired=1;
[self.myView addGestureRecognizer:tapRecognizer];

然后我在视图中添加一个UIToolBar带有按钮的:

UIToolbar *topBar = [[UIToolbar alloc ]initWithFrame:CGRectMake(0, 0, self.myView.frame.size.width, 44)];
topBar.barStyle = UIBarStyleBlackTranslucent;

UIBarButtonItem *logout = [[UIBarButtonItem alloc] initWithTitle:@"Logout" style:UIBarButtonItemStyleBordered target:self action:@selector(logout)];

[topBar setItems:@[logout] animated:NO];

我在单击注销按钮时遇到问题,并且我的点击识别器会触发而不是我的注销操作。如果我单击并按住,则会触发注销操作(我猜点击识别器失败了,所以让按钮操作触发)。

按下按钮时如何不触发手势识别器?

4

3 回答 3

4

刚遇到同样的问题。因为我不想引入容器视图(UIToolbar 应该覆盖我现有的视图)。在 Patrick.Ji 粗略指出的帮助下,我想出了这个:

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

不要忘记将手势的代表设置为self

- (void)viewDidLoad {
    [super viewDidLoad];
    UITapGestureRecognizer *mainTapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(mainTapGesture:)];
    mainTapGestureRecognizer.delegate = self;
    [self.view addGestureRecognizer:mainTapGestureRecognizer];
}
于 2013-02-19T05:12:11.833 回答
0

检查点击识别器中的视图。如果它是您的注销按钮,请让触摸无法通过super.

或者,确保您的工具栏不是您的视图的子视图。相反,拥有一个包含工具栏和内容视图的容器视图,并将手势识别器添加到此内容视图。

于 2012-12-20T18:20:36.550 回答
-1

实现 UIGestureRecognizer 的这个委托方法(记得设置你的 tapRecognizer.delegate = self)

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

    if ([touch.view isKindOfClass:[UIBarButtonItem class]]) 
    {
        return NO;
    }
    return YES;
}
于 2012-12-23T05:43:31.433 回答