登录 立即注册
金钱:

Code4App-iOS开发-iOS 开源代码库-iOS代码实例搜索-iOS特效示例-iOS代码例子下载-Code4App.com

Swfit AKSwiftSlideMenu

[复制链接]
来自: 李世铿 分类: iOS精品源码 上传时间: 2016-6-23 15:46:19
Tag:menu swift 菜单 侧滑 Slide

项目介绍:

AKSwiftSlideMenu

Slide Menu (Drawer) in Swift 2
Why to use a library everytime?
Let's create our own Slide Menu in Swift 2.


1. 在Xcode7下创建Swift项目

2. 在UIViewController上设计menu

Menu UIViewController

Declaration of Variables and Protocols (Delegate) :

[Objective-C] 查看源文件 复制代码
protocol SlideMenuDelegate {
    func slideMenuItemSelectedAtIndex(index : Int32)
}

class MenuViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
    @IBOutlet var tblMenuOptions : UITableView!
    @IBOutlet var btnCloseMenuOverlay : UIButton!
    var arrayMenuOptions = [Dictionary<String,String>]()
    var btnMenu : UIButton!
    var delegate : SlideMenuDelegate?
}
Following method is for updating the Items in the Menu :

    func updateArrayMenuOptions(){
        arrayMenuOptions.append(["title":"Home", "icon":"HomeIcon"])
        arrayMenuOptions.append(["title":"Play", "icon":"PlayIcon"])
        arrayMenuOptions.append(["title":"Camera", "icon":"CameraIcon"])
        
        tblMenuOptions.reloadData()
    }
Following method is for click event and animation :

@IBAction func onCloseMenuClick(button:UIButton!){
    btnMenu.tag = 0
    
    if (self.delegate != nil) {
        var index = Int32(button.tag)
        if(button == self.btnCloseMenuOverlay){
            index = -1
        }
        delegate?.slideMenuItemSelectedAtIndex(index)
    }
    
    UIView.animateWithDuration(0.3, animations: { () -> Void in
        self.view.frame = CGRectMake(-UIScreen.mainScreen().bounds.size.width, 0, UIScreen.mainScreen().bounds.size.width,UIScreen.mainScreen().bounds.size.height)
        self.view.layoutIfNeeded()
        self.view.backgroundColor = UIColor.clearColor()
        }, completion: { (finished) -> Void in
            self.view.removeFromSuperview()
            self.removeFromParentViewController()
    })
}

3. Now we will create a Base UIViewController to use anywhere in the project which control the delegate of menu.

First we will create this 3 lines Drawer Icon via Code

Preview_Slider_Drawer_ICon

func addSlideMenuButton(){
    let btnShowMenu = UIButton(type: UIButtonType.System)
    btnShowMenu.setImage(self.defaultMenuImage(), forState: UIControlState.Normal)
    btnShowMenu.frame = CGRectMake(0, 0, 30, 30)
    btnShowMenu.addTarget(self, action: #selector(BaseViewController.onSlideMenuButtonPressed(_:)), forControlEvents: UIControlEvents.TouchUpInside)
    let customBarItem = UIBarButtonItem(customView: btnShowMenu)
    self.navigationItem.leftBarButtonItem = customBarItem;
}
func defaultMenuImage() -> UIImage {
    var defaultMenuImage = UIImage()
    UIGraphicsBeginImageContextWithOptions(CGSizeMake(30, 22), false, 0.0)
        
    UIColor.blackColor().setFill()
    UIBezierPath(rect: CGRectMake(0, 3, 30, 1)).fill()
    UIBezierPath(rect: CGRectMake(0, 10, 30, 1)).fill()
    UIBezierPath(rect: CGRectMake(0, 17, 30, 1)).fill()
        
    UIColor.whiteColor().setFill()
    UIBezierPath(rect: CGRectMake(0, 4, 30, 1)).fill()
    UIBezierPath(rect: CGRectMake(0, 11,  30, 1)).fill()
    UIBezierPath(rect: CGRectMake(0, 18, 30, 1)).fill()
        
    defaultMenuImage = UIGraphicsGetImageFromCurrentImageContext()
        
    UIGraphicsEndImageContext()
       
    return defaultMenuImage;
}
Delegate (Protocol) method call :

func slideMenuItemSelectedAtIndex(index: Int32) {
    let topViewController : UIViewController = self.navigationController!.topViewController!
    print("View Controller is : \(topViewController) \n", terminator: "")
    switch(index){
    case 0:
        print("Home\n", terminator: "") //Move to that view
        break
    case 1:
        print("Play\n", terminator: "") //Move to that view
        break
    case 2:
        print("Camera\n", terminator: "") //Move to that view
        break
    default:
        print("default\n", terminator: "")
    }
}
To open a view controller by identifier :

Set the Restoration Identifier and Storyboard Identifier. If current view is open then we will not open it once again for that we have to check via Restoration Identifier.

func openViewControllerBasedOnIdentifier(strIdentifier:String){
    let destViewController : UIViewController = self.storyboard!.instantiateViewControllerWithIdentifier(strIdentifier)
        
    let topViewController : UIViewController = self.navigationController!.topViewController!
    
    if (topViewController.restorationIdentifier! == destViewController.restorationIdentifier!){
        print("Same VC")
    } else {
        self.navigationController!.pushViewController(destViewController, animated: true)
    }
}
4. Now We will assign this drawer to any of the UIViewController

We have to use only one method to add drawer (slide menu) self.addSlideMenuButton()

import UIKit

class ViewController: BaseViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        self.addSlideMenuButton()
    }
}


DEMO   下载:

相关源码推荐:

我来说两句
*滑动验证:
所有评论(7)
王颖博 2016-6-23 17:36:36
感谢分享,Code4App有你更精彩
回复
大洋 2016-6-23 19:41:58
强烈支持楼主ing……
回复
bbbbbbbbb1 2016-6-24 10:35:13
楼主威武啊,Code4App有你更给力!
回复
kengsir 2016-6-27 18:07:05
支持,感谢,祝code4app越来越好~
回复
phoiu 2016-6-27 18:09:02
写的真的很不错
回复
hellokenken 2016-6-27 18:10:43
好好 学习了 确实不错
回复
BlueManlove 2016-6-27 18:12:26
不错不错,楼主辛苦了。。。
回复
code4app热心网友 2016-8-20 09:39:38
淡定,淡定,淡定……
回复
dengmeng1226 2016-8-22 16:57:42
我只是路过打酱油的。
回复
提取码:  下载次数:12 状态:已购或VIP 售价:0(原价:10)金钱 下载权限:初级码农 
1742 0 12
联系我们
首页/微信公众账号投稿

帖子代码编辑/版权问题

QQ:435399051,742864542

如何获得代码达人称号?

代码贡献英雄榜
用户名 下载数
通过邮件订阅最新 Code4App 信息
上一条 /4 下一条
联系我们
关闭
合作电话:
13802416937
Email:
435399051@qq.com
商务市场合作/投稿
问题反馈及帮助
联系我们

广告投放| 广东互联网违法和不良信息举报中心|中国互联网举报中心|Github|申请友链|手机版|Code4App ( 粤ICP备15117877号-1 )

快速回复 返回顶部 返回列表