UIGestureRecognizer Interference

I just ran into a bug in iOS 5.1, where the UIPanGestureRecognizer that is installed automatically for a UISplitViewController interferes with being able to drag a slider. This manifests itself as that you can drag the slider left, but attempting to drag it right causes it to move a few pixels and then stop tracking.

In trying to track this down, I ran across a number of other posts by users who were trying to figure out what recognisers were active, so I thought I’d share this code snippet.

[cc lang=”objc”]
NSLog(@”Recognizers:”);
NSMutableArray *views = [self.view.window.subviews mutableCopy];
while (views.count > 0) {
UIView *view = [views objectAtIndex:0];
[views removeObjectAtIndex:0];
if (view.gestureRecognizers != nil) {
for (UIGestureRecognizer *recognizer in view.gestureRecognizers) {
NSLog(@” Found %@”, recognizer);
NSString *viewPath = [NSString stringWithCString:class_getName([view class]) encoding:NSUTF8StringEncoding];
UIView *sv = view.superview;
while (sv != nil) {
viewPath = [NSString stringWithFormat:@”%s -> %@”, class_getName([sv class]), viewPath];
sv = sv.superview;
}
NSLog(@” On view: %@”, viewPath);
}
}
[views addObjectsFromArray:view.subviews];
}
[/cc]

Drop this code into a method that runs when you suspect you have gesture recogniser interference, and have a look at the list. (You will also need to import objc/runtime.h).

This will walk all the views in the current window hierarchy, and print out a list of gesture recognisers on any view that has them configured. This isn’t a solution in itself, but at least it can give you a list of things to look at. In my case, once I saw the UISplitViewController had a recogniser configured, a little light bulb went off in my head and on a hunch I disabled it, and my problem went away. This, and some subsequent Googling, led me here, which describes the problem perfectly.