swift编写简单的左右菜单栏

时间:2018-03-29 02:17:11
【文件属性】:

文件名称:swift编写简单的左右菜单栏

文件大小:73KB

文件格式:ZIP

更新时间:2018-03-29 02:17:11

swift

swift编写简单的左右菜单栏 // // MenuViewController.swift // Menu // // Created by innovane on 14-6-23. // Copyright (c) 2014年 innovane. All rights reserved. // import UIKit let kICSDrawerControllerLeftViewInitialOffset : CGFloat = 60.0 let kICSDrawerControllerDrawerDepth : CGFloat = 200.0 enum ICSDrawerControllerState : Int { case MenuControllerStateOpening case MenuControllerStateClosing } class MenuViewController: UIViewController { //用户点击center var tapGestureRecognizer : UITapGestureRecognizer! // 用户拖动视图 var panGestureRecognizer : UIPanGestureRecognizer! // 用户touch的点位置 var panGestureStartLocation : CGPoint! // 左边控制器 var leftViewController : UIViewController! //中间控制器 var centerViewController : UIViewController! var drawerState : ICSDrawerControllerState! init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: NSBundle?) { super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil) // Custom initialization } override func viewDidLoad() { super.viewDidLoad() self.drawerState = ICSDrawerControllerState.MenuControllerStateClosing; } override func viewDidAppear(animated: Bool) { super.viewDidAppear(animated) // 将左边控制器加入导航栏中。 if self.leftViewController != nil{ if (self.leftViewController.view.superview == nil){ self.addChildViewController(self.leftViewController) self.view.insertSubview(self.leftViewController.view, atIndex:0) } } if self.centerViewController != nil{ if (self.centerViewController.view.superview == nil){ self.addChildViewController(self.centerViewController) self.view.addSubview(self.centerViewController.view) } } // 添加用户拖动事件。 self.panGestureRecognizer = UIPanGestureRecognizer() self.panGestureRecognizer.addTarget(self,action:"panGestureRecognized:"); self.centerViewController.view.addGestureRecognizer(self.panGestureRecognizer) } // 用户拖动视图调用代理方法。 func panGestureRecognized(panGestureRecognizer:UIPanGestureRecognizer){ // 用户对视图操控的状态。 var state = panGestureRecognizer.state; var location = panGestureRecognizer.locationInView(self.view) var velocity = panGestureRecognizer.velocityInView(self.view) switch (state) { case UIGestureRecognizerState.Began: // 记录用户开始点击的位置 self.panGestureStartLocation = location; println("Began") break; case UIGestureRecognizerState.Changed: println("Changed") var c = self.centerViewController.view.frame if (panGestureRecognizer.translationInView(self.centerViewController.view).x > 0){ if (self.drawerState == ICSDrawerControllerState.MenuControllerStateClosing){ c.origin.x = location.x - self.panGestureStartLocation.x; } }else if (panGestureRecognizer.translationInView(self.centerViewController.view).x > -kICSDrawerControllerDrawerDepth){ if (self.drawerState == ICSDrawerControllerState.MenuControllerStateOpening){ c.origin.x = panGestureRecognizer.translationInView(self.centerViewController.view).x+kICSDrawerControllerDrawerDepth } } self.centerViewController.view.frame = c ; break; case UIGestureRecognizerState.Ended: var c = self.centerViewController.view.frame // 表示用户需要展开 if (location.x - self.panGestureStartLocation.x > kICSDrawerControllerLeftViewInitialOffset){ self.didOpen() }else{ if (c.origin.x < (kICSDrawerControllerDrawerDepth - 40)){ self.didClose() }else{ self.didOpen() } } break; default: break; } } // 移除点击事件,添加拖动事件 func tapGestureRecognized(tapGestureRecognizer : UITapGestureRecognizer){ self.didClose(); } // 菜单栏打开 func didOpen(){ var c = self.centerViewController.view.frame c.origin.x = kICSDrawerControllerDrawerDepth; UIView.animateWithDuration(0.7,delay:0,usingSpringWithDamping:0.5,initialSpringVelocity:1.0,options:UIViewAnimationOptions.AllowUserInteraction,animations:{ self.centerViewController.view.frame = c ; },completion: { (finished: Bool) -> Void in }) self.drawerState = ICSDrawerControllerState.MenuControllerStateOpening //增加点击事件 if (self.tapGestureRecognizer == nil){ self.tapGestureRecognizer = UITapGestureRecognizer() self.tapGestureRecognizer.addTarget(self,action:"tapGestureRecognized:"); } self.centerViewController.view.addGestureRecognizer(self.tapGestureRecognizer) } // 菜单关闭 func didClose(){ if (self.drawerState == ICSDrawerControllerState.MenuControllerStateOpening){ self.drawerState = ICSDrawerControllerState.MenuControllerStateClosing self.centerViewController.view.removeGestureRecognizer(self.tapGestureRecognizer) } var c = self.centerViewController.view.frame c.origin.x = 0 UIView.animateWithDuration(0.5,delay:0,usingSpringWithDamping:0.9,initialSpringVelocity:1.0,options:UIViewAnimationOptions.AllowUserInteraction,animations:{ self.centerViewController.view.frame = c ; },completion: { (finished: Bool) -> Void in }) } }


【文件预览】:
Menu
----.git()
--------index(1KB)
--------hooks()
--------config(137B)
--------description(73B)
--------refs()
--------branches()
--------logs()
--------objects()
--------info()
--------COMMIT_EDITMSG(15B)
--------HEAD(23B)
----Menu.xcodeproj()
--------project.xcworkspace()
--------project.pbxproj(15KB)
--------xcuserdata()
----.DS_Store(6KB)
----MenuTests()
--------MenuTests.swift(875B)
--------Info.plist(751B)
----Menu()
--------hamburger@2x.png(1KB)
--------MenuViewController.swift(6KB)
--------.DS_Store(6KB)
--------AppDelegate.swift(4KB)
--------Images.xcassets()
--------Info.plist(876B)
--------hamburger.png(16KB)

网友评论

  • 挺好的,值得参考