Ios – How to set up an UIScreenEdgePanGestureRecognizer using Interface Builder

interface-builderiosobjective cxcode

I can't get UIScreenEdgePanGestureRecognizer to work when I create when I add it to my view controller using Interface Builder, so I'm asking here to establish whether I'm doing something wrong or if there is a bug in Xcode.

Here are the steps to reproduce:

  • Create a new Xcode project using the "Single View Application" template for iOS.
  • Add a UIView to the main view controller by dragging one from the Object Library in Interface Builder
  • Add a UIScreenEdgePanGestureRecognizer to the view by dragging one from the Object Library in Interface Builder
  • Ensure that the gesture recogniser is enabled and that an edge is selected:

enter image description here

  • Open the assistant editor for the ViewController class and ctrl-drag from the UIScreenEdgePanGestureRecognizer to the ViewController's implementation block to create a new IBAction
    ` Add a breakpoint in the action's method body to test if the edge pan gesture is being recognized

The resulting code is as follows:

Code example

If I run the application on my device (iPhone 6 running iOS 8.02) the breakpoint does not get hit when I do an edge swipe.

Is there something I'm missing?

UPDATE: this was filed as a bug with Apple (rdar://18582306) on 08-Oct-2014 and still isn't resolved in Xcode 6.4 (6E35b)

Best Answer

I set up a project to test your question and found the same issue you did. Here are the scenarios I set up to test:

  • I did exactly as you did, using Interface Builder, to build the screen edge gesture. The only difference in my code is that I put a line of code in the selector so the debugger would have something to stop on. The debugger failed to halt exactly as you found.

    -(void)handlePan:(id)sender
    {
        NSString *test = @"";
    }
    
  • I then created an additional gesture using the pinch gesture on the same view using Interface Builder and I was able to get the debugger to halt within that selector. So Interface Builder seems to be able to build other gestures correctly.

  • I then created the screen edge gesture manually using the following code and it worked as expected.

In the ViewController.h file I included the UIGestureRecognizerDelegate.

@interface ViewController : UIViewController <UIGestureRecognizerDelegate>
@end

In the ViewController.m file I implemented the gesture manually.

#import "ViewController.h"

@interface ViewController ()

-(void)handlePan:(id)sender;

@end

@implementation ViewController

- (void)viewDidLoad 
{
    [super viewDidLoad];
    UIScreenEdgePanGestureRecognizer *pan = [[UIScreenEdgePanGestureRecognizer alloc] initWithTarget:self
                                                                                              action:@selector(handlePan:)];
    [pan setEdges:UIRectEdgeLeft];
    [pan setDelegate:self];
    [self.view addGestureRecognizer:pan];
}

-(void)handlePan:(id)sender
{
    NSString *test = @"";
}

@end

I ended up with the same conclusion you did - there seems to be something wrong with the Interface Builder's implementation of the UIScreenEdgePanGestureRecognizer.