iOS,側滑(最簡單效果卻很好的側滑功能實現
阿新 • • 發佈:2019-02-04
很多時候側滑功能都會被用到。下面來介紹一下,很簡單,效果卻很好。
1.先按照圖片新建LeftViewController,MainViewController(當然請先下載ReSideMenu類:http://download.csdn.net/detail/qq_27325349/9351409)
2.在ViewController.m中新增圖下程式碼:
#import "ViewController.h" #import "LeftViewController.h" #import "MainViewController.h" #import "RESideMenu.h" @interface ViewController () @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; self.view.backgroundColor=[UIColor greenColor]; UIButton *button=[UIButton buttonWithType:UIButtonTypeContactAdd]; button.frame=CGRectMake(100, 100, 60, 60); [button addTarget:self action:@selector(click) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:button]; } -(void)click { MainViewController *main=[[MainViewController alloc]init]; LeftViewController *left=[[LeftViewController alloc]init]; RESideMenu *sideMenuViewController=[[RESideMenu alloc]initWithContentViewController:main leftMenuViewController:left rightMenuViewController:nil]; self.view.window.rootViewController=sideMenuViewController; NSLog(@"000"); }
3.在MainViewController.m中新增如下程式碼
#import "MainViewController.h" #import "RESideMenu.h" @interface MainViewController () @end @implementation MainViewController - (void)viewDidLoad { [super viewDidLoad]; self.view.backgroundColor=[UIColor purpleColor]; UIButton *avator=[UIButton buttonWithType:UIButtonTypeCustom]; [avator setBackgroundImage:[UIImage imageNamed:@"111"] forState:UIControlStateNormal]; avator.frame=CGRectMake(0, 44, 50, 50); [avator addTarget:self action:@selector(presentLeftMenuViewController:) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:avator]; }
4.在LeftViewController.m中新增如下程式碼
#import "LeftViewController.h" @interface LeftViewController () @end @implementation LeftViewController - (void)viewDidLoad { [super viewDidLoad]; self.view.backgroundColor=[UIColor blueColor]; UILabel *label=[[UILabel alloc]init]; [label setText:@"左側頁面"]; label.frame=CGRectMake(50, 44, 80, 40); [self.view addSubview:label]; }
5.效果圖如下:(主頁面和左頁面的具體展示可以根據自己需要寫,點選頭像側滑和滑動側滑都OK的,功能都封裝好了)