欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

使用SWRevealViewController实现侧滑菜单 objective-cxcode 

程序员文章站 2022-07-15 16:35:11
...

1.导入SWRevealViewController

由于该库是用OC写的,所以在swift上使用还需要一些步骤;

1.1 下载SWRevealViewController到本地,然后打开SWRevealViewController文件夹你会看到两个文件

使用SWRevealViewController实现侧滑菜单
            
    
    
        objective-cxcode 

1.2 把这个两个文件拖到你的xcode项目中;然后xocde就会自动弹出个框,询问你是否创建OC bridging header文件,这里选择 Create Bridging Header;苹果官方推荐通过header file文件来让Swift项目使用OC

使用SWRevealViewController实现侧滑菜单
            
    
    
        objective-cxcode 

1.3 然后xcode就会创建一个 项目名-Bridging-Header.h文件,打开该文件,输入:

#import "SWRevealViewController.h"
  • 1

1.4 选择项目名文件,打开Building Settings选项卡 直接搜索关键词 bridging,在 Objective-C Bridging Header 右边对应的方框中双击,复制之前的 Yoper-Bridging-Header.h名称并粘贴按回车键就完成了,

如图:

使用SWRevealViewController实现侧滑菜单
            
    
    
        objective-cxcode 

这样OC库就导入完成了!

2.使用SWRevealViewController (storyboard方式)

2.1 拖拽一个新的View Controller到面板上,并设置class 为SWRevealViewController

使用SWRevealViewController实现侧滑菜单
            
    
    
        objective-cxcode 

2.2 创建菜单 View Controller,这里可以为任何类型的,

  • 2.2.1 创建完成后,左键点击SWRevealViewController的View Controller并按住control不放,

使用SWRevealViewController实现侧滑菜单
            
    
    
        objective-cxcode 

  • 2.2.2 拖动到该菜单ViewController,松开鼠标 会出现一个选择面板
  • 2.2.3 选择 reveal view controller set controller

使用SWRevealViewController实现侧滑菜单
            
    
    
        objective-cxcode 

  • 2.2.4 点击两个ViewController 之间的链接线,在属性面板设置identifier 为sw_rear(不可为其他字符)

使用SWRevealViewController实现侧滑菜单
            
    
    
        objective-cxcode 

2.3 链接要显示侧滑菜单的ViewController ; 如2.2.1 2.2.2 2.2.3 步骤,把SWRevealViewController 与你要显示的ViewController 链接起来,然后对链接线的identifier属性设置为sw_front(代表左侧菜单)

结果如图,这里要注意一个问题,就是指向第一个显示ViewController的箭头 指向了SWRevealViewController; 因为我在实际开发中发现不指向这个ViewController 侧滑菜单不出现,不知道是什么原因 
使用SWRevealViewController实现侧滑菜单
            
    
    
        objective-cxcode 

2.4 在代码设置SWRevealViewController的一些属性和滑动事件

 //侧滑菜单
if(self.revealViewController() != nil) {
     menuItem.target = self.revealViewController()
     //添加点击事件
     menuItem.action = #selector(SWRevealViewController.revealToggle(_:))
     //添加触摸事件
     self.view.addGestureRecognizer(self.revealViewController().panGestureRecognizer())
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

如果你想判断当前的菜单是否在打开状态,可以用如下代码,

 if(self.revealViewController() != nil) {
                    if self.revealViewController().frontViewPosition != FrontViewPosition.left {
                        self.revealViewController().revealToggle(self.menuItem)
                    }
                }
  • 1
  • 2
  • 3
  • 4
  • 5

FrontViewPosition.left代表左侧菜单

3. 运行结果

使用SWRevealViewController实现侧滑菜单
            
    
    
        objective-cxcode 

相关标签: objective-c xcode