快速滑出栏的问题

时间:2015-12-16 08:56:34

标签: ios swift uiviewcontroller

过去两天我想在我的项目中使用swift2实现幻灯片菜单。我不想使用任何框架并由我自己做。 所以,我找到了一个很棒的教程http://www.raywenderlich.com/78568/create-slide-out-navigation-panel-swift#comments

主要问题 - 我想做一个更简单的问题。我不需要动物的任何其他类 - 在我的实现中,我想设计一个带标签的简单单元格。我只需要左菜单。

所以,我开始重写代码,并坚持这样的错误。调试完成,但是,点击MenuBtn时没有任何反应。

这是我的ContainerViewContainer

import UIKit
import QuartzCore

enum SlideOutState {
   case BothCollapsed
   case LeftPanelExpanded
}

class ContainerViewController: UIViewController {

var centerNavigationController: UINavigationController!
var centerViewController: ViewController!

var currentState: SlideOutState = .BothCollapsed
var leftViewController: 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)

}

}



extension ContainerViewController: CenterViewControllerDelegate {

func toggleLeftPanel() {

    let notAlreadyExpanded = (currentState != .LeftPanelExpanded)

    if notAlreadyExpanded {
        addLeftPanelViewController()
    }

    animateLeftPanel(shouldExpand: notAlreadyExpanded)

}


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

func addLeftPanelViewController() {

    if (leftViewController == nil) {
        leftViewController = UIStoryboard.leftViewController()

        //leftViewController!.animals = Animal.allCats()

        addChildSidePanelController(leftViewController!)
    }
}


func addChildSidePanelController(sidePanelController: SidePanelViewController) {
    view.insertSubview(sidePanelController.view, atIndex: 0)

    addChildViewController(sidePanelController)
    sidePanelController.didMoveToParentViewController(self)
}

func animateLeftPanel(shouldExpand shouldExpand: Bool) {

    if (shouldExpand) {
        currentState = .LeftPanelExpanded

        animateCenterPanelXPosition(targetPosition: CGRectGetWidth(centerNavigationController.view.frame) - centerPanelExpandedOffset)
    } else {
        animateCenterPanelXPosition(targetPosition: 0) { finished in
            self.currentState = .BothCollapsed

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

}

//animation
func animateCenterPanelXPosition(targetPosition 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)
}



}


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 centerViewController() -> ViewController? {
    return mainStoryboard().instantiateViewControllerWithIdentifier("CenterViewController") as? ViewController
}


}

这是我的CenterViewController-在我的项目中称为ViewController

    import UIKit
    import QuartzCore


    @objc
    protocol CenterViewControllerDelegate {
        optional func toggleLeftPanel()
        optional func collapseSidePanels()
    }

//*********************************************Button Action************************************
    var delegate: CenterViewControllerDelegate?



    @IBAction func MenuBtn(sender: AnyObject) {
        delegate?.toggleLeftPanel!()
    }

这是SlidePanelViewController

import UIKit
class SidePanelViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {


    @IBOutlet weak var tableView: UITableView!
    var menu = Array<String>()

    override func viewDidLoad() {
        super.viewDidLoad()

        menu.append("View 1")
        menu.append("View 2")

        tableView.dataSource = self
        tableView.delegate = self

        // Do any additional setup after loading the view.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


    // MARK: Table View Data Source


        func numberOfSectionsInTableView(tableView: UITableView) -> Int {
            return 1
        }

        func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
            return menu.count
        }

        func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
            let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as? UITableViewCell


            cell!.textLabel?.text = menu[indexPath.row]


            return cell!
    }

}

而且,正如我之前所说的,当我点击MenuBtn时没有发生任何事情 我能忘记什么?

P.S。我不打算实施整个教程,我将停留在“小狗到右边......”的段落中。

0 个答案:

没有答案