滑动手势在未调用时从右向左移动

时间:2014-10-24 18:06:56

标签: swift slide

我在制作它时只能从左向右滑动。当我尝试从左向右滑动到侧栏菜单时,它会打开但是当我从右向左滑动时,它会留下一个空置的黑色空间。我希望它能够从左到右为幻灯片客户工作,打开我的侧栏菜单。如果您需要更多信息,请与我们联系。什么都有帮助谢谢!

导入UIKit 导入QuartzCore

enum SlideOutState {     案例LeftCollapsed     case LeftPanelExpanded }

类ContainerViewController:UIViewController,CenterViewControllerDelegate,UIGestureRecognizerDelegate {

var centerNavigationController: UINavigationController!
var centerViewController: CenterViewController!
var currentState: SlideOutState = .LeftCollapsed {
    didSet {
        let shouldShowShadow = currentState != .LeftCollapsed
        showShadowForCenterViewController(shouldShowShadow)
    }
}
var leftViewController: SidePanelViewController?

let centerPanelExpandedOffset: CGFloat = 60

override init() {
    super.init(nibName: nil, bundle: nil)
}

required init(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)
}

override func viewDidLoad() {
    super.viewDidLoad()

    centerViewController = UIStoryboard.centerViewController()
    centerViewController.delegate = self

    // wrap the centerViewController in a navigation controller, so we can push views to it
    // and display bar button items in the navigation bar
    centerNavigationController = UINavigationController(rootViewController: centerViewController)
    view.addSubview(centerNavigationController.view)
    addChildViewController(centerNavigationController)

    centerNavigationController.didMoveToParentViewController(self)

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

func handlePanGesture(识别器:UIPanGestureRecognizer){         let gestureIsDraggingFromLeftToRight =(recognizer.velocityInView(view).x> 0)

    switch(recognizer.state) {
    case .Began:
        if (currentState == .LefCollapsed) {
            if (gestureIsDraggingFromLeftToRight) {
                addLeftPanelViewController()
            }

            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)
        }
    default:
        break
    }
}

}

1 个答案:

答案 0 :(得分:3)

在Began案例中添加else-Statement,其中u禁用识别器。在休息之前再次启用它。

 func handlePanGesture(recognizer: UIScreenEdgePanGestureRecognizer) {
        let gestureIsDraggingFromLeftToRight = (recognizer.velocityInView(view).x > 0)

        switch(recognizer.state) {
        case .Began:
            if (currentState == .BothCollapsed) {
                if (gestureIsDraggingFromLeftToRight) {
                    addLeftPanelViewController()
                } else {
                    recognizer.enabled = false
                }
                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)
            }
        default:
            recognizer.enabled = true
            break
        }
    }