在iOS開發(fā)中經(jīng)常碰到類似QQ界面中左滑顯示視圖的界面,實現(xiàn)這種效果的話可以用一個第三方(RESideMenu)RESideMenu是github上比較出名的一個開源庫秧耗,主要是實現(xiàn)側(cè)滑菜單.
我們想實現(xiàn)這樣的功能:
點擊導(dǎo)航欄左邊按鈕,當(dāng)前內(nèi)容視圖能夠向右滑出一半车猬,于此同時尺锚,屏幕左側(cè)會出現(xiàn)一個菜單視圖,效果如下圖:
話不多說上代碼
首先用cocopods ?或者在項目中導(dǎo)入第三方RESideMenu
AppDelegate.h中:導(dǎo)入頭文件?RESideMenu.h
@property(strong,nonatomic)RESideMenu *sideMenu;
AppDelegate.m中:
#define SCREEN_W [UIScreen mainScreen].bounds.size.width
///屏幕高度的宏
#define SCREEN_H? [UIScreen mainScreen].bounds.size.height
///適配x軸的宏
#define FIT_X(w) (SCREEN_W / 375. * (w))
///適配y軸的宏
#define FIT_Y(h) (SCREEN_H / 667. * (h))
@property (nonatomic,strong)LeftViewController *leftVC; //左側(cè)菜單控制器
@property(nonatomic,strong)ViewController *vc;
@property(nonatomic,strong)UINavigationController *nav;
//左側(cè)菜單
-(LeftViewController *)leftVC{
if (!_leftVC) {
_leftVC = [[LeftViewController alloc]init];
}
return _leftVC;
}
//導(dǎo)航控制器
-(UINavigationController *)nav{
if (!_nav) {
_nav=[[UINavigationController alloc]initWithRootViewController:self.vc];
}
return _nav;
}
//主控制器
-(ViewController *)vc{
if (!_vc) {
_vc = [[ViewController alloc]init];
_vc.view.backgroundColor=[UIColor whiteColor];
}
return _vc;
}
//側(cè)滑控制器
-(RESideMenu *)sideMenu{
if (!_sideMenu) {
//左滑視圖
_sideMenu = [[RESideMenu alloc]initWithContentViewController:self.nav leftMenuViewController:self.leftVC rightMenuViewController:nil];
//設(shè)置內(nèi)容試圖不可縮放
_sideMenu.scaleContentView = NO;
//設(shè)置左滑視圖與主界面的間隔
_sideMenu.contentViewInPortraitOffsetCenterX = FIT_X(100);
}
return _sideMenu;
}
self.window.rootViewController=self.sideMenu;
ViewController.m中
viewDidLoad:
//設(shè)置導(dǎo)航欄左按鈕
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"左滑" style:UIBarButtonItemStylePlain target:self action:@selector(headBtnHandle:)];
//按鈕觸發(fā)方法
-(void)headBtnHandle:(id)sender{
AppDelegate *appDele =(AppDelegate *) [UIApplication sharedApplication].delegate;
[appDele.sideMenu presentLeftMenuViewController];
}