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

Posted

技术标签:

【中文标题】未调用时滑动手势从右向左移动【英文标题】:Slide Gesture moving from right to left when not called 【发布时间】:2014-10-24 18:06:56 【问题描述】:

我无法让它只从左向右滑动。当我尝试从左向右滑动进入侧栏菜单时,它会打开,但是当我从右向左滑动时,它会留下一个未占用的黑色空间。我希望它只适用于从左到右的幻灯片来打开我的侧栏菜单。如果您需要更多信息,请与我们联系。任何事情都会有所帮助,谢谢!

导入 UIKit 导入 QuartzCore

枚举 SlideOutState 案例 LeftCollapsed 案例 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) 让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】:

在 Began 案例中添加一个 else 语句,您可以在其中禁用识别器。 在中断之前再次启用它。

 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
        
    

【讨论】:

这正是我所需要的!我知道它必须在这个区域,只是不知道如何称呼它。谢谢!!

以上是关于未调用时滑动手势从右向左移动的主要内容,如果未能解决你的问题,请参考以下文章

Android中的手势滑动

我的应用程序使用片段,但我需要从左到右从右向左滑动

在 iOS 中做一个摩擦屏幕的手势?

使用 animate() 从右向左滑动 div

jQuery从右向左滑动

如何使div从右向左滑动