这是我之前写的项目中用到过的一个侧滑菜单库,今天又把他做成了是一个Demo,拿来和大家分享。

Swift左右侧滑菜单 1

Swift左右侧滑菜单,源代码Demo免费下载 - 敏捷大拇指 - Swift左右侧滑菜单 1


首先,新建一个Podfile文件,pod 'KGFloatingDrawer' 这个库

[Swift] 纯文本查看 复制代码
platform :ios, '9.0'
use_frameworks!

target 'Swift-左右侧滑菜单' do
pod 'KGFloatingDrawer', '~> 0.2.0'
end


然后在你的AppDelegate中import KGFloatingDrawer

[Swift] 纯文本查看 复制代码
import UIKit
import KGFloatingDrawer

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?

    /*你storyboard的名字*/
    let kKGDrawersStoryboardName = "Main"
    /*想设置的背景图片的名字*/
    let backgroundImgName = "login3"
    /*主视图StoryboardId*/
    let kKGDrawerSettingsViewControllerStoryboardId = "center"
    /*左视图StoryboardId*/
    let kKGLeftDrawerStoryboardId = "left"
    /*右视图StoryboardId*/
    let kKGRightDrawerStoryboardId = "right"

    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

        window = UIWindow(frame: UIScreen.mainScreen().bounds)

        window?.rootViewController = drawerViewController

        window?.makeKeyAndVisible()

        return true
    }

    /*
     *****************
     *      *        *
     *****************
     */
    private var _drawerViewController: KGDrawerViewController?
    var drawerViewController: KGDrawerViewController {
        get {
            if let viewController = _drawerViewController {
                return viewController
            }
            return prepareDrawerViewController()
        }
    }

    func prepareDrawerViewController() -> KGDrawerViewController {
        let drawerViewController = KGDrawerViewController()

        drawerViewController.centerViewController = drawerSettingsViewController()
        drawerViewController.leftViewController = leftViewController()
        drawerViewController.rightViewController = rightViewController()
        drawerViewController.backgroundImage = UIImage(named: backgroundImgName)

        _drawerViewController = drawerViewController

        return drawerViewController
    }

    private func drawerStoryboard() -> UIStoryboard {
        let storyboard = UIStoryboard(name: kKGDrawersStoryboardName, bundle: nil)
        return storyboard
    }

    private func viewControllerForStoryboardId(storyboardId: String) -> UIViewController {
        let viewController: UIViewController = drawerStoryboard().instantiateViewControllerWithIdentifier(storyboardId)
        return viewController
    }

    func drawerSettingsViewController() -> UIViewController {
        let viewController = viewControllerForStoryboardId(kKGDrawerSettingsViewControllerStoryboardId)
        return viewController
    }


    private func leftViewController() -> UIViewController {
        let viewController = viewControllerForStoryboardId(kKGLeftDrawerStoryboardId)
        return viewController
    }

    private func rightViewController() -> UIViewController {
        let viewController = viewControllerForStoryboardId(kKGRightDrawerStoryboardId)
        return viewController
    }

    func toggleLeftDrawer(sender:AnyObject, animated:Bool) {
        _drawerViewController?.toggleDrawer(.Left, animated: true, complete: { (finished) -> Void in
            // do nothing
        })
    }

    func toggleRightDrawer(sender:AnyObject, animated:Bool) {
        _drawerViewController?.toggleDrawer(.Right, animated: true, complete: { (finished) -> Void in
            // do nothing
        })
    }

    private var _centerViewController: UIViewController?
    var centerViewController: UIViewController {
        get {
            if let viewController = _centerViewController {
                return viewController
            }
            return drawerSettingsViewController()
        }
        set {
            if let drawerViewController = _drawerViewController {
                drawerViewController.closeDrawer(drawerViewController.currentlyOpenedSide, animated: true) { finished in }
                if drawerViewController.centerViewController != newValue {
                    drawerViewController.centerViewController = newValue
                }
            }
            _centerViewController = newValue
        }
    }

    /*
     *****************
     *      *        *
     *****************
     */


之后像我图片这样创建一个主视图,一个左视图,和一个右视图,并设置设置storyboard ID。

Swift左右侧滑菜单 2

Swift左右侧滑菜单,源代码Demo免费下载 - 敏捷大拇指 - Swift左右侧滑菜单 2

侧滑菜单

最后给左右两个按钮绑定滑动事件

[Swift] 纯文本查看 复制代码
import UIKit
import KGFloatingDrawer

class ViewController: UIViewController {

    /*左滑动按钮*/
    @IBAction func left(sender: UIBarButtonItem) {

        let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
        appDelegate.drawerViewController.toggleDrawer(KGDrawerSide.Left, animated: true) { (finished) -> Void
            in
        }

    }

    /*右滑动按钮*/
    @IBAction func right(sender: UIBarButtonItem) {

        let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
        appDelegate.drawerViewController.toggleDrawer(KGDrawerSide.Right, animated: true) { (finished) -> Void
            in
        }

    }


OK,搞定!




源代码Demo免费下载

Swift-左右侧滑菜单.zip (3.32 MB, 下载次数: 1)