Swift-左右侧滑菜单

作者: 无穷369 | 来源:发表于2016-07-28 18:19 被阅读504次
    Swift-左右侧滑菜单.gif

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

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

    platform :ios, '9.0'
    use_frameworks!
    
    target 'Swift-左右侧滑菜单' do
    pod 'KGFloatingDrawer', '~> 0.2.0'
    end
    

    然后在你的AppDelegate中import KGFloatingDrawer

    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

    侧滑菜单.png

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

    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地址https://pan.baidu.com/s/1kU8AQ5x

    相关文章

      网友评论

        本文标题:Swift-左右侧滑菜单

        本文链接:https://www.haomeiwen.com/subject/qdmajttx.html