UIGestureRecognizer blocks subview for handling touch events

后端 未结 10 1017
北恋
北恋 2020-11-28 19:32

I\'m trying to figure out how this is done the right way. I\'ve tried to depict the situation: \"enter

相关标签:
10条回答
  • 2020-11-28 20:07

    I was displaying a dropdown subview that had its own tableview. As a result, the touch.view would sometimes return classes like UITableViewCell. I had to step through the superclass(es) to ensure it was the subclass I thought it was:

    -(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
    {
        UIView *view = touch.view;
        while (view.class != UIView.class) {
            // Check if superclass is of type dropdown
            if (view.class == dropDown.class) { // dropDown is an ivar; replace with your own
                NSLog(@"Is of type dropdown; returning NO");
                return NO;
            } else {
                view = view.superview;
            }
        }
    
        return YES;
    }
    
    0 讨论(0)
  • 2020-11-28 20:10

    I was also doing a popover and this is how I did it

    func didTap(sender: UITapGestureRecognizer) {
    
        let tapLocation = sender.locationInView(tableView)
    
        if let _ = tableView.indexPathForRowAtPoint(tapLocation) {
            sender.cancelsTouchesInView = false
        }
        else {
            delegate?.menuDimissed()
        }
    }
    
    0 讨论(0)
  • 2020-11-28 20:12

    The blocking of touch events to subviews is the default behaviour. You can change this behaviour:

    UITapGestureRecognizer *r = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(agentPickerTapped:)];
    r.cancelsTouchesInView = NO;
    [agentPicker addGestureRecognizer:r];
    
    0 讨论(0)
  • 2020-11-28 20:14

    You can turn it off and on.... in my code i did something like this as i needed to turn it off when the keyboard was not showing, you can apply it to your situation:

    call this is viewdidload etc:

    NSNotificationCenter    *center = [NSNotificationCenter defaultCenter];
    [center addObserver:self selector:@selector(notifyShowKeyboard:) name:UIKeyboardDidShowNotification object:nil];
    [center addObserver:self selector:@selector(notifyHideKeyboard:) name:UIKeyboardWillHideNotification object:nil];
    

    then create the two methods:

    -(void) notifyShowKeyboard:(NSNotification *)inNotification 
    {
        tap.enabled=true;  // turn the gesture on
    }
    
    -(void) notifyHideKeyboard:(NSNotification *)inNotification 
    {
        tap.enabled=false;  //turn the gesture off so it wont consume the touch event
    }
    

    What this does is disables the tap. I had to turn tap into a instance variable and release it in dealloc though.

    0 讨论(0)
提交回复
热议问题