第三方库KGFloatingDrawer实现抽屉
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了第三方库KGFloatingDrawer实现抽屉相关的知识,希望对你有一定的参考价值。
Xcode中使用第三方函数库一般使用的是cocoapods,所以最好先安装一个cocoapods,这样方便使用第三方函数库。
cocoapods的安装方式在下面的链接中http://blog.csdn.net/iunion/article/details/17010267
首先在在AppDelegate中引入import KGFloatingDrawer
然后添加如下代码:
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
window = UIWindow(frame: UIScreen.mainScreen().bounds)
//设置根视图
window?.rootViewController = drawerViewController
self.drawerViewController.backgroundImage=UIImage(named: "bg")
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 = drawerViewController
let animator=_drawerViewController?.animator
animator?.animationDuration=0.7
animator?.initialSpringVelocity=1.0
animator?.springDamping=5.0
return drawerViewController
}
private func drawerStoryboard() -> UIStoryboard {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
return storyboard
}
private func viewControllerForStoryboardId(storyboardId: String) -> UIViewController {
let viewController: UIViewController = drawerStoryboard().instantiateViewControllerWithIdentifier(storyboardId)
return viewController
}
//主视图
func drawerSettingsViewController() -> UIViewController {
let viewController = viewControllerForStoryboardId("center")
return viewController
}
//左视图
private func leftViewController() -> UIViewController {
let viewController = viewControllerForStoryboardId("left")
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
}
}
点击按钮的方法
@IBAction func btnlist(sender: AnyObject) {
let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
appDelegate.drawerViewController.toggleDrawer(KGDrawerSide.Left, animated:true) { (finished) -> Void
in
}
}
以上是关于第三方库KGFloatingDrawer实现抽屉的主要内容,如果未能解决你的问题,请参考以下文章