Swift:滑出菜单

时间:2015-02-27 00:02:48

标签: ios swift menu

我正在Swift中开发一个iOS应用程序。我想显示左侧滑出菜单,所以我按照Ray Wenderlich的教程(http://www.raywenderlich.com/78568/create-slide-out-navigation-panel-swift#comments

本教程适用于左右滑出菜单,但我只想要左边的菜单。所以我使用左边的部分;但是,它只显示黑色菜单而不是我想要的左菜单。如何正确显示菜单?

1 个答案:

答案 0 :(得分:3)

这是修改后只显示左侧部分的网站代码,看到它,它会删除右侧面板打开的所有部分。

import UIKit
import QuartzCore

enum SlideOutState {
   case Collapsed
   case Expanded
}

class ContainerViewController: UIViewController, CenterViewControllerDelegate, UIGestureRecognizerDelegate {

   var centerNavigationController: UINavigationController!
   var centerViewController: CenterViewController!

   var mainPanelExpandedOffset: CGFloat = 518

   var currentState: SlideOutState = .Collapsed {
      didSet {
          let shouldShowShadow = currentState != .Collapsed
          showShadowForCenterViewController(shouldShowShadow)
      }
   }

   var leftViewController: SidePanelViewController?
   var rightViewController: SidePanelViewController?

   let centerPanelExpandedOffset: CGFloat = 60

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

// MARK: CenterViewController delegate methods

func toggleLeftPanel() {
    let notAlreadyExpanded = (currentState != .Expanded)

    if notAlreadyExpanded {
        addLeftPanelViewController()
    }

    animateLeftPanel(shouldExpand: notAlreadyExpanded)
}


func collapseSidePanels() {
    switch (currentState) {
    case .Expanded:
        toggleLeftPanel()
    default:
        break
    }
}

func addLeftPanelViewController() {
    if (leftViewController == nil) {
        leftViewController = UIStoryboard.leftViewController()
        leftViewController!.animals = Animal.allCats()

        addChildSidePanelController(leftViewController!)
    }
}

func addChildSidePanelController(sidePanelController: SidePanelViewController) {
    sidePanelController.delegate = centerViewController

    view.insertSubview(sidePanelController.view, atIndex: 0)

    addChildViewController(sidePanelController)
    sidePanelController.didMoveToParentViewController(self)
}

func animateLeftPanel(#shouldExpand: Bool) {
    if (shouldExpand) {
        currentState = .Expanded
        animateCenterPanelXPosition(targetPosition: CGRectGetWidth(leftViewController!.view.frame) - 100)

    } else {
        animateCenterPanelXPosition(targetPosition: 0) { finished in
            self.currentState = .Collapsed

            self.leftViewController?.view.removeFromSuperview()
            self.leftViewController = nil;
        }
    }
}


func animateCenterPanelXPosition(#targetPosition: CGFloat, completion: ((Bool) -> Void)! = nil) {
    UIView.animateWithDuration(0.5, delay: 0, usingSpringWithDamping: 0.8, initialSpringVelocity: 0, options: .CurveEaseInOut, animations: {
        self.centerNavigationController.view.frame.origin.x = targetPosition
        }, completion: completion)
}

func showShadowForCenterViewController(shouldShowShadow: Bool) {
    if (shouldShowShadow) {
        centerNavigationController.view.layer.shadowOpacity = 0.8
    } else {
        centerNavigationController.view.layer.shadowOpacity = 0.0
    }
}

// MARK: Gesture recognizer

func handlePanGesture(recognizer: UIPanGestureRecognizer) {

    let gestureIsDraggingFromLeftToRight = (recognizer.velocityInView(view).x > 0)

    switch(recognizer.state) {

    case .Changed:

        if (gestureIsDraggingFromLeftToRight && currentState == .Expanded){
            recognizer.view!.center.x = recognizer.view!.center.x + recognizer.translationInView(view).x
            recognizer.setTranslation(CGPointZero, inView: view)
            currentState = .Collapsed
        }

    case .Ended:
        if (gestureIsDraggingFromLeftToRight) {

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

private extension UIStoryboard {
     class func mainStoryboard() -> UIStoryboard { return UIStoryboard(name: "Main", bundle: NSBundle.mainBundle()) }

     class func leftViewController() -> SidePanelViewController? {
        return mainStoryboard().instantiateViewControllerWithIdentifier("LeftViewController") as? SidePanelViewController
     }

    class func rightViewController() -> SidePanelViewController? {
        return   mainStoryboard().instantiateViewControllerWithIdentifier("RightViewController") as? SidePanelViewController
    }

    class func centerViewController() -> CenterViewController? {
        return mainStoryboard().instantiateViewControllerWithIdentifier("CenterViewController") as? CenterViewController
    }
}

您也可以尝试Creating a Sidebar Menu Using SWRevealViewController in Swift我认为它也是一个好地方。

我希望这可以帮到你