简体   繁体   中英

UISplitViewController swipe gesture interferes with other swipe gesture

I'm using a UISplitViewController and one of the detail view controllers contains a view which has a UIPanGestureRecognizer added to it. When I swipe this view in the detail view controller, the gesture is recognized, but the swipe gesture recognizer of the split view controller interferes with it; the master view controller is displayed and the gesture recognizer from the detail controller is ignored.

Implementing and debugging the shouldRecognizeSimultaneouslyWithGestureRecognizer method from the UIGestureRecognizerDelegate shows two UIPanGestureRecognizer objects: one from the detail view controller and the one from the split view controller, so I'm certain they are interfering with each other.

When I set presentsWithGesture = NO on the split view controller, the gesture recognizer inside the detail view controller does work. But that disables the gesture recognizer on the split view controller, so it's not really a solution to the problem.

I've also tried disabling the gesture recognizer on the split view controller, only when I need the other gesture recognizer to work, but it seems that it is not possible to set presentsWithGesture once the split view controller has become visible.

I've also tried to disable the default gesture on the split view controller and adding a custom gesture which I can control, but it didn't work. I tried using the target and action from the split view controller barbutton on the gesture, but it doesn't work. Calling toggleMasterVisible: on the split view controller isn't an option either because it's part of the private api.

Does anyone have any suggestions on how to handle this?

I would suggest that you disable the UISplitViewController pan gesture when you need the other one to work. This should do it:

for (UIGestureRecognizer* recognizer in [splitViewController gestureRecognizers]) {
    if ([recognizer isKindOfClass:[UIPanGestureRecognizer class]]) {
        [recognizer setEnabled:NO];
    }
}

You probably don't want to search for it each time, so I would store a reference to that gesture recognizer when the view loads, then just disable and enable as appropriate:

on viewDidLoad:

for (UIGestureRecognizer* recognizer in [splitViewController gestureRecognizers]) {
    if ([recognizer isKindOfClass:[UIPanGestureRecognizer class]]) {
        [self setSplitViewPanGesture:recognizer];
    }
}

later:

[self.splitViewPanGesture setEnabled:NO];

and later:

[self.splitViewPanGesture setEnabled:YES];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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