How to block a gesture from superview to subview?

前端 未结 6 1930
感动是毒
感动是毒 2020-12-30 11:32

I\'m writing a module that everytime I swipe on a view, two sub views with a half size of the view will be added. Those subviews have their own gestures (eg: pan,...). The f

6条回答
  •  南方客
    南方客 (楼主)
    2020-12-30 12:06

    You have to implement the UIGestureRecognizerDelegate method:

    - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer;
    

    And add your controller as the delegate of the gesture recognizers. Then, when two gesture recognizers respond to a gesture, this method will be called and here you can implement the logic you want for your app.

    In the interface declaration of the controller you have to type:

    @interface testcViewController () 
    

    Then, when creating the gesture recognizer:

    UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipe)];
    swipe.direction = UISwipeGestureRecognizerDirectionDown;
    swipe.delegate = self;
    [self.view addGestureRecognizer:swipe];
    

    And then, finally, you add this method to the controller:

    - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
    {
        BOOL shouldInteract = NO;
        //Here you decide whether or not the two recognizers whould interact.
        return shouldInteract;
    }
    

    EDIT You can also implement

    - (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer;
    

    And here, detect if you have already presented the subviews, and block any gesture you want.

提交回复
热议问题