Swift UIPanGestureRecognizer仅从右到左

时间:2014-11-10 19:29:05

标签: ios swift swipe

我正在使用Ray Wenderlich的教程构建一个滑出菜单,无法弄清楚如何禁用从左到右的平移但从右到左?我删除addLeftPanelViewController()但它仍然显示平移,只是无法正常工作。我不希望任何动作,或从左到右取消隐藏。

感谢您的帮助。

override func viewDidLoad() {
    let panGestureRecognizer = UIPanGestureRecognizer(target: self, action: "handlePanGesture:")
            centerNavigationController.view.addGestureRecognizer(panGestureRecognizer)
}



let gestureIsDraggingFromLeftToRight = (recognizer.velocityInView(view).x > 0)
 
switch(recognizer.state) {
  case .Began:
    if (currentState == .BothCollapsed) {
      if (gestureIsDraggingFromLeftToRight) {
        addLeftPanelViewController()
      } else {
        addRightPanelViewController()
      }
 
      showShadowForCenterViewController(true)
    }
  case .Changed:
    recognizer.view!.center.x = recognizer.view!.center.x + recognizer.translationInView(view).x
    recognizer.setTranslation(CGPointZero, inView: view)
  case .Ended:
    if (leftViewController != nil) {
      // animate the side panel open or closed based on whether the view has moved more or less than halfway
      let hasMovedGreaterThanHalfway = recognizer.view!.center.x > view.bounds.size.width
      animateLeftPanel(shouldExpand: hasMovedGreaterThanHalfway)
    } else if (rightViewController != nil) {
      let hasMovedGreaterThanHalfway = recognizer.view!.center.x < 0
      animateRightPanel(shouldExpand: hasMovedGreaterThanHalfway)
    }
  default:
    break
}

2 个答案:

答案 0 :(得分:2)

试试这个

在SlideOutState枚举

上添加Expending
enum SlideOutState {
    case BothCollapsed
    case SettingPanelExpanded
    case Expanding

}

然后

switch(recognizer.state) {
        case .Began:
            if (currentState == .BothCollapsed) {
                if (gestureIsDraggingFromLeftToRight) {
                    addSettingPanelViewController()
                    showShadowForCenterViewController(true)
                    currentState = .Expanding
                }

            }
            if (currentState == .SettingPanelExpanded) {
                if (gestureIsDraggingFromLeftToRight==false) {
                    currentState = .Expanding
                }
            }
        case .Changed:
            if (currentState == .Expanding){

                println(recognizer.velocityInView(view).x)
                recognizer.view!.center.x = recognizer.view!.center.x + recognizer.translationInView(view).x
                recognizer.setTranslation(CGPointZero, inView: view)
            }
        case .Ended:
            if (settingViewController != nil) {
                // animate the side panel open or closed based on whether the view has moved more or less than halfway
                let hasMovedGreaterThanHalfway = recognizer.view!.center.x > view.bounds.size.width
                animateSettingPanel(shouldExpand: hasMovedGreaterThanHalfway)
            }
        default:
            break
        }

答案 1 :(得分:0)

switch语句换成if条件:

if currentState == .LeftPanelExpanded || gestureIsDraggingFromLeftToRight {
  //your existing switch statement
}