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
}
}