//聯(lián)系人:石虎QQ: 1224614774昵稱:嗡嘛呢叭咪哄
UITabBarController做QQ側(cè)滑菜單效果:
首先要了解UITabBarController的層級結(jié)構(gòu):
UITabBarController加載的其它UIViewController的View都是被添加在UITransitionView上(這是一個私有API)菠赚,UITransitionView在self.view的0層何恶,UITabBar在的第一層茬贵。
所以我的思路是這樣的:
UITransitionView與UITabBar轉(zhuǎn)移到一個新的View1上去,作為滑動的部分渤弛;
在View1與self.view之間再添加一個View2,作為菜單的容器;
給View1添加相應(yīng)的手勢響應(yīng)圃伶;
代碼:
#import@protocolSlideTab_VC_TCVDelegate@optional-(void)didOpenMenu:(UIView*)menu;-(void)didCloseMenu:(UIView*)menu;@end@interfaceSlideTab_VC:UITabBarController@property(strong,nonatomic)UIView*mMenuV;@property(weak,nonatomic)id mDelegate;-(void)openMenu;-(void)closeMenu;@end
#import"SlideTab_VC.h"#define DEVICE_W [UIScreen mainScreen].bounds.size.width@interfaceSlideTab_VC(){CGFloat_centerMaxX;}@property(strong,nonatomic)UIView*mTransitionView;@property(strong,nonatomic)UITapGestureRecognizer*mTapGester;@property(assign,nonatomic)BOOLmMenuIsOpen;@property(strong,nonatomic)UITabBar*mTabBar;@end@implementationSlideTab_VC- (void)viewDidLoad {? ? [superviewDidLoad];// Do any additional setup after loading the view.self.view.backgroundColor = [UIColorpurpleColor];? ? _mMenuIsOpen =NO;? ? _centerMaxX = DEVICE_W*3/2-80.0f;? ? [selfinitMTransitionView];? ? [selfaddGestureForMTransitionView];}#pragma mark 菜單懶加載-(void)setMMenuV:(UIView*)mMenuV{if(mMenuV!=nil) {? ? ? ? _mMenuV = mMenuV;? ? ? ? [self.view insertSubview:_mMenuV atIndex:0];? ? }}#pragma mark 打開菜單-(void)openMenu{CGPointcenter =self.mTransitionView.center;? ? center.x = _centerMaxX;? ? [UIViewanimateWithDuration:0.15f animations:^{self.mTransitionView.center = center;if(_mDelegate !=nil&& [_mDelegate respondsToSelector:@selector(didOpenMenu:)]) {? ? ? ? [_mDelegate didOpenMenu:_mMenuV];? ? }? ? }];? ? [selfmTransitionSubViewsEnable:NO];}#pragma mark 關(guān)閉菜單-(void)closeMenu{CGPointcenter =self.mTransitionView.center;? ? center.x = DEVICE_W/2;? ? [UIViewanimateWithDuration:0.15f animations:^{self.mTransitionView.center = center;? ? } completion:^(BOOLfinished) {? ? ? ? [selfmTransitionSubViewsEnable:YES];if(_mDelegate !=nil&& [_mDelegate respondsToSelector:@selector(didCloseMenu:)]) {? ? ? ? [_mDelegate didCloseMenu:_mMenuV];? ? }? ? }];}#pragma mark Transition用戶交互使能-(void)mTransitionSubViewsEnable:(BOOL)enable{for(UIView*tmpinself.mTransitionView.subviews) {? ? ? ? tmp.userInteractionEnabled = enable;? ? }if(enable) {? ? ? ? [self.mTransitionView removeGestureRecognizer:_mTapGester];? ? }else{? ? ? ? [self.mTransitionView addGestureRecognizer:_mTapGester];? ? }}#pragma mark 配置mTransitionView-(void)initMTransitionView{for(UIView*tmpinself.view.subviews) {? ? ? ? [tmp removeFromSuperview];? ? ? ? [self.mTransitionView addSubview:tmp];? ? }? ? [self.view addSubview:self.mTransitionView];}#pragma mark 拖動手勢動作-(void)panAction:(UIPanGestureRecognizer*)pan{CGPointlocation = [pan translationInView:pan.view.superview];CGPointcenter =self.mTransitionView.center;if(pan.state==UIGestureRecognizerStateEnded) {if(center.x<_centerMaxX*0.5+DEVICE_W*0.25){? ? ? ? ? ? [selfcloseMenu];? ? ? ? }else{? ? ? ? ? ? [selfopenMenu];? ? ? ? }? ? }elseif(pan.state==UIGestureRecognizerStateChanged){if(location.x<0) {//向左滑center.x = center.x+location.x<=DEVICE_W/2? DEVICE_W/2: center.x+location.x;? ? ? ? }else{? ? ? ? ? ? center.x = center.x+location.x>=_centerMaxX? _centerMaxX : center.x+location.x;? ? ? ? }self.mTransitionView.center = center;? ? ? ? [pan setTranslation:CGPointMake(0,0) inView:pan.view.superview];? ? }}#pragma mark 添加手勢-(void)addGestureForMTransitionView{UIPanGestureRecognizer*pan = [[UIPanGestureRecognizeralloc]initWithTarget:selfaction:@selector(panAction:)];? ? [_mTransitionView addGestureRecognizer:pan];? ? _mTapGester = [[UITapGestureRecognizeralloc]initWithTarget:selfaction:@selector(tapAction)];}#pragma mark--懶加載-(UIView*)mTransitionView{if(_mTransitionView==nil) {? ? ? ? _mTransitionView = [[UIViewalloc]initWithFrame:self.view.bounds];? ? }return_mTransitionView;}-(void)tapAction{CGFloatx = _mTransitionView.center.x;if(x>=_centerMaxX) {? ? ? ? [selfcloseMenu];? ? }}@end
謝謝!!!