How do I set up an UIScreenEdgePanGestureRecognizer using Interface Builder?

后端 未结 7 1408
自闭症患者
自闭症患者 2021-02-06 23:48

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 wh

7条回答
  •  走了就别回头了
    2021-02-07 00:45

    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 
    @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.

提交回复
热议问题