我正在使用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
}

最佳答案

尝试这个

在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
        }

关于ios - Swift UIPanGestureRecognizer仅从右到左,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/26851464/

10-11 14:49