Swift左右侧滑菜单,源代码Demo免费下载

分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友 微信微信
查看查看140 回复回复1 收藏收藏 分享淘帖 转播转播 分享分享 微信
查看: 140|回复: 1
收起左侧

Swift左右侧滑菜单,源代码Demo免费下载

[复制链接]
SwiftRobot 发表于 2016-7-29 17:17:01 | 显示全部楼层 |阅读模式
快来登录
获取最新的苹果动态资讯
收藏热门的iOS等技术干货
拷贝下载Swift Demo源代码

这是我之前写的项目中用到过的一个侧滑菜单库,今天又把他做成了是一个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)

都看到这里了,就把这篇资料推荐给您的好朋友吧,让他们也感受一下。

回帖是一种美德,也是对楼主发帖的尊重和支持。

*声明:敏捷大拇指是全球最大的Swift开发者社区、苹果粉丝家园、智能移动门户,所载内容仅限于传递更多最新信息,并不意味赞同其观点或证实其描述;内容仅供参考,并非绝对正确的建议。本站不对上述信息的真实性、合法性、完整性做出保证;转载请注明来源并加上本站链接,敏捷大拇指将保留所有法律权益。如有疑问或建议,邮件至marketing@swifthumb.com

*联系:微信公众平台:“swifthumb” / 腾讯微博:@swifthumb / 新浪微博:@swifthumb / 官方QQ一群:343549891(满) / 官方QQ二群:245285613 ,需要报上用户名才会被同意进群,请先注册敏捷大拇指

嗯,不错!期待更多好内容,支持一把:
支持敏捷大拇指,用支付宝支付10.24元 支持敏捷大拇指,用微信支付10.24元

评分

参与人数 1金钱 +10 收起 理由
Anewczs + 10 推送上首页了~分享到朋友圈微博QQ空间吧~.

查看全部评分

本帖被以下淘专辑推荐:

瑶池 发表于 2016-8-4 21:12:39 | 显示全部楼层
居然没人点赞!
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

做任务,领红包。
我要发帖

分享扩散

都看到这里了,就把这资料推荐给您的好朋友吧,让他们也感受一下。
您的每一位朋友访问此永久链接后,您都将获得相应的金钱积分奖励
热门推荐

合作伙伴

Swift小苹果

  • 北京治世天下科技有限公司
  • ©2014-2016 敏捷大拇指
  • 京ICP备14029482号
  • Powered by Discuz! X3.1 Licensed
  • swifthumb Wechat Code
  •   
快速回复 返回顶部 返回列表