UIScreenEdgePanGestureRecognizer:顶部和底部边缘

这是否有可能使UIScreenEdgePanGestureRecognizer处理来自顶部 (或底部 )边缘的事件?

UIScreenEdgePanGestureRecognizer *gestureRecognizer = [[UIScreenEdgePanGestureRecognizer alloc] initWithTarget:self action:@selector(userDidPan:)]; gestureRecognizer.edges = UIRectEdgeTop; // UIRectEdgeBottom [self.view addGestureRecognizer:gestureRecognizer]; 

问候。

状态栏必须隐藏起来才能工作。 在你的UIViewController ,覆盖(BOOL)prefersStatusBarHidden()并返回YES。

但是在ios8(没有在ios7中testing)。 当设备方向为“左侧风景”(当设备方向为“右侧风景”时无法识别顶部手势)时,底部手势无法识别。 我正在调查,但看起来像一个bug。

顶部和底部边缘手势从屏幕边缘触发20像素。

默认情况下,状态栏是可见的,并占据前20像素的空间,所以你必须隐藏状态栏才能使用这些(顶部/机器人)UIScreenEdgePanGestureRecognizers,否则代理不会被调用。

步骤1:

图片

第2步:

添加以下代码:

ViewController.h

 @interface ViewController : UIViewController<UIGestureRecognizerDelegate> 

ViewController.m

 - (void)viewDidLoad { [super viewDidLoad]; UIScreenEdgePanGestureRecognizer *topEdgeGesture = [[UIScreenEdgePanGestureRecognizer alloc] initWithTarget:self action:@selector(handleTopEdgeGesture:)]; topEdgeGesture.edges = UIRectEdgeTop; //UIRectEdgeBottom topEdgeGesture.delegate = self; [self.view addGestureRecognizer:topEdgeGesture]; } - (BOOL) prefersStatusBarHidden { return YES; } - (void)handleTopEdgeGesture:(UIScreenEdgePanGestureRecognizer *)gesture { NSLog(@"TOP"); } 

迅速:

 class ViewController: UIViewController,UIGestureRecognizerDelegate { override func viewDidLoad() { super.viewDidLoad() var edgeGesture : UIScreenEdgePanGestureRecognizer = UIScreenEdgePanGestureRecognizer(target: self, action:"handleTopEdgeGesture:") edgeGesture.edges = UIRectEdge.Top edgeGesture.delegate = self self.view.addGestureRecognizer(edgeGesture) } func handleTopEdgeGesture(gesture:UIScreenEdgePanGestureRecognizer) { println("TOP") } override func prefersStatusBarHidden() -> Bool { return true } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } }