简体   繁体   English

UITapGestureRecognizer与CCButton冲突

[英]UITapGestureRecognizer conflicting with CCButton

I've looked around on Stack for solutions to this and found quite a few results, but I'm still unable to fix my problem, mainly because I'm working on a CCNode opposed to some UIView. 我已经在Stack上四处寻找解决方案,并找到了很多结果,但是我仍然无法解决问题,主要是因为我正在使用CCUI而不是某些UIView。

Anyways, my CCButton works find without the UITapGestureRecognizer , but when I implement it, it overrides my button press. 无论如何,我的CCButton可以在没有UITapGestureRecognizer情况下找到,但是当我实现它时,它将覆盖我的按钮按下。 Obviously I would like to avoid this. 显然,我想避免这种情况。

My code is below. 我的代码如下。 My swipe gestures work perfectly, but the tap interferes with my button. 我的滑动手势效果很好,但是点击会干扰我的按钮。 I'm not sure if I'm adding the gesture recognizer to the wrong view, because - (BOOL)gesturerecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch never runs, which is where I would put in my exception. 我不确定是否将手势识别器添加到错误的视图中,因为- (BOOL)gesturerecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch永远不会运行,这就是我的例外情况。 If anyone can give me some code/ideas on how to fix this it would be greatly appreciated, thanks! 如果有人可以给我一些如何解决此问题的代码/想法,将不胜感激,谢谢!

-(void)didLoadFromCCB   

    UITapGestureRecognizer * gridTapped = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(gridTapped)];
    [[[CCDirector sharedDirector] view] addGestureRecognizer:gridTapped];

    //this works totally fine
    UISwipeGestureRecognizer * swipeLeft= [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipeLeft)];
    swipeLeft.direction = UISwipeGestureRecognizerDirectionLeft;
    [[[CCDirector sharedDirector] view] addGestureRecognizer:swipeLeft];
}


-(void)gridTapped {
    //this works!
    CCLOG(@"Grid tapped");
}

-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
    //This never runs
    CCLOG(@"gesture recognized");
    if ((touch.view == mainMenuButton)) {
        return NO;
    }
    return YES;
}

Try setting the UITapGestureRecognizer delegate: 尝试设置UITapGestureRecognizer委托:

gridTapped.delegate = self;

You'll need to declare the VC class as implementing <UITapGestureRecognizerDelegate> 您需要将VC类声明为实现<UITapGestureRecognizerDelegate>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM