A swipe menu is a common feature in many applications, and there are many ways to implement it. This article describes how to use a SWRevealViewController to implement a swipe menu.
1. Import SWRevealViewController
Since the library is written in OC, using it on Swift requires a few steps;
- 1.1 Download the SWRevealViewController locally, then open the SWRevealViewController folder and you’ll see two files
- 1.2 Drag the two files into your Xcode project; And then xOCde will automatically pop up a box asking you if you want to Create OC Bridging header files, so Create Bridging header; Apple officially recommends using OC for Swift projects via header file
- 1.3 Xcode will then create one
Project name - Bridging - Header. H
Open the file and type:
#import "SWRevealViewController.h"
Copy the code
- 1.4 Select the project name file and open it
Building Settings TAB
Just search for bridging, double-click in the box to the right of objective-C bridging Header, copy the name of the previous Oper -bridge-header. h and paste it and hit Enter,
As shown in figure:
QQ20170916-144803.png
Then the OC library import is complete!
2. Use SWRevealViewController (storyboard)
- 2.1 Drag a new View Controller onto the panel and set class to
SWRevealViewController
- 2.2 Create menu View Controller, here can be any type,
- 2.2.1 After the creation is complete, left-click
SWRevealViewController
View Controller and hold downcontrol
Not to put,
- 2.2.1 After the creation is complete, left-click
QQ20170916-150516.png
- 2.2.2 Drag to the menu ViewController, release the mouse and a selection panel will appear
- 2.2.3 choice
reveal view controller set controller
- 2.2.4 Click the link line between the two ViewControllers and set it in the properties panel
identifier
为sw_rear
(Cannot contain other characters)
- 2.3 Link the ViewController to display the slide menu; Such as
2.2.1
2.2.2
2.2.3
Step, link the SWRevealViewController to the ViewController you want to display, and then link the link lineidentifier
Property set tosw_front
(Represents the menu on the left)
The result is shown, and one thing to notice here is that the arrow pointing to the first show ViewController points to the SWRevealViewController; Because I found in actual development that the scroll menu doesn’t show up without pointing to this ViewController, I don’t know why
QQ20170916-152135.png
- 2.4 Setting in code
SWRevealViewController
Some properties and slide events of
/ / side menu if (self revealViewController ()! = nil) {menuItem. Target = self. RevealViewController () / / add a click event menuItem. Action = # the selector (SWRevealViewController revealToggle (_)) / / add touch events self.view.addGestureRecognizer(self.revealViewController().panGestureRecognizer()) }Copy the code
If you want to check whether the current menu is open, you can use the following code:
if(self.revealViewController() ! = nil) { if self.revealViewController().frontViewPosition ! = FrontViewPosition.left { self.revealViewController().revealToggle(self.menuItem) } }Copy the code
Frontviewposition. left represents the left menu