代码之家  ›  专栏  ›  技术社区  ›  AYMADA

iOS检测到屏幕左边缘外的拖动

  •  4
  • AYMADA  · 技术社区  · 12 年前

    在iPad中,当你将手指放在屏幕的顶部或底部边缘外,然后将其拖到屏幕上时,菜单就会显示出来。我如何实现这一点?

    2 回复  |  直到 4 年前
        1
  •  7
  •   aksh1t    12 年前
    • 有一个专门的手势识别器类,在 iOS 7 。这是 UIScreenEdgePanGestureRecognizer 。它的文档是 here 。看看。

    • 要在模拟器中测试这一点,只需从边缘附近开始拖动(约15点)。

    • 此外,您还必须为每条边创建一个手势识别器。你不能把边缘或在一起,所以 UIRectEdgeAll 不起作用。

    有一个简单的例子 here 。希望这有帮助!

        2
  •  1
  •   Harsh    12 年前

    嗯,你可以这样做,这个例子是这样的,你希望你的平移手势只有在用户从屏幕右侧向内滑动20px时才起作用

    首先,将手势添加到窗口

    - (void)addGestures {  
    if (!_panGesture) {      
    _panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePanGesture:)]; 
    [_panGesture setDelegate:self]; 
    [self.view addGestureRecognizer:_panGesture];   
    }
    }
    

    添加后,检查您收到的触摸是否为平移手势,然后相应地执行操作

    - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {    
    CGPoint point = [touch locationInView:self.view];
    if (gestureRecognizer == _panGesture) {     
    return [self slideMenuForGestureRecognizer:gestureRecognizer withTouchPoint:point]; 
    } 
    return YES;
    }
    

    以下是如何检查触摸是否包含在您希望的区域中

     -(BOOL)isPointContainedWithinBezelRect:(CGPoint)point {
       CGRect leftBezelRect; 
       CGRect tempRect; 
     //this will be the width between CGRectMaxXEdge and the screen offset, thus identifying teh region
       CGFloat bezelWidth =20.0;
       CGRectDivide(self.view.bounds, &leftBezelRect, &tempRect, bezelWidth, CGRectMaxXEdge); 
       return CGRectContainsPoint(leftBezelRect, point);
       }