Uitabbarcontroller makes QQ sideslip menu effect:
First, understand the hierarchy of uitabbarcontroller:
The views of other uiviewcontrollers loaded by uitabbarcontroller are added to uitransitionview (this is a private API), and uitransitionview is in self The 0 layer of view and the uitabbar are on the first layer of.
So my idea is this:
Uitransitionview and uitabbar are transferred to a new view1 as the sliding part;
Between view1 and self Add another view2 between views as the container of the menu;
Add corresponding gesture response to view1;
code:
#import <UIKit/UIKit.h>
@protocol SlideTab_VC_TCVDelegate <NSObject>
@optional
-(void)didOpenMenu:(UIView*)menu;
-(void)didCloseMenu:(UIView*)menu;
@end
@interface SlideTab_VC : UITabBarController
@property(strong, nonatomic)UIView *mMenuV;
@property(weak, nonatomic)id <SlideTab_VC_TCVDelegate> mDelegate;
-(void)openMenu;
-(void)closeMenu;
@end
#import "SlideTab_VC.h"
#define DEVICE_W [UIScreen mainScreen].bounds.size.width
@interface SlideTab_VC ()<UITabBarDelegate>{
CGFloat _centerMaxX;
}
@property(strong, nonatomic)UIView *mTransitionView;
@property(strong, nonatomic)UITapGestureRecognizer *mTapGester;
@property(assign, nonatomic)BOOL mMenuIsOpen;
@property(strong, nonatomic)UITabBar *mTabBar;
@end
@implementation SlideTab_VC
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor = [UIColor purpleColor];
_mMenuIsOpen = NO;
_centerMaxX = DEVICE_W*3/2-80.0f;
[self initMTransitionView];
[self addGestureForMTransitionView];
}
#Pragma mark menu lazy loading
-(void)setMMenuV:(UIView *)mMenuV{
if (mMenuV!=nil) {
_mMenuV = mMenuV;
[self.view insertSubview:_mMenuV atIndex:0];
}
}
#Pragma mark open menu
-(void)openMenu{
CGPoint center = self.mTransitionView.center;
center.x = _centerMaxX;
[UIView animateWithDuration:0.15f animations:^{
self.mTransitionView.center = center;
if (_mDelegate != nil&& [_mDelegate respondsToSelector:@selector(didOpenMenu:)]) {
[_mDelegate didOpenMenu:_mMenuV];
}
}];
[self mTransitionSubViewsEnable:NO];
}
#Pragma mark close menu
-(void)closeMenu{
CGPoint center = self.mTransitionView.center;
center.x = DEVICE_W/2;
[UIView animateWithDuration:0.15f animations:^{
self.mTransitionView.center = center;
} completion:^(BOOL finished) {
[self mTransitionSubViewsEnable:YES];
if (_mDelegate != nil&& [_mDelegate respondsToSelector:@selector(didCloseMenu:)]) {
[_mDelegate didCloseMenu:_mMenuV];
}
}];
}
#Pragma mark transition user interaction enable
-(void)mTransitionSubViewsEnable:(BOOL)enable{
for (UIView *tmp in self.mTransitionView.subviews) {
tmp.userInteractionEnabled = enable;
}
if (enable) {
[self.mTransitionView removeGestureRecognizer:_mTapGester];
}else{
[self.mTransitionView addGestureRecognizer:_mTapGester];
}
}
#Pragma mark configuring mtransitionview
-(void)initMTransitionView{
for (UIView *tmp in self.view.subviews) {
[tmp removeFromSuperview];
[self.mTransitionView addSubview:tmp];
}
[self.view addSubview:self.mTransitionView];
}
#Pragma mark drag gesture
-(void)panAction:(UIPanGestureRecognizer*)pan{
CGPoint location = [pan translationInView:pan.view.superview];
CGPoint center = self.mTransitionView.center;
if (pan.state==UIGestureRecognizerStateEnded) {
if (center.x<_centerMaxX*0.5+DEVICE_W*0.25){
[self closeMenu];
}else{
[self openMenu];
}
}else if(pan.state==UIGestureRecognizerStateChanged){
If (location. X < 0) {// slide left
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 add gesture
-(void)addGestureForMTransitionView{
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(panAction:)];
[_mTransitionView addGestureRecognizer:pan];
_mTapGester = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction)];
}
#Pragma mark -- lazy loading
-(UIView *)mTransitionView{
if (_mTransitionView==nil) {
_mTransitionView = [[UIView alloc]initWithFrame:self.view.bounds];
}
return _mTransitionView;
}
-(void)tapAction{
CGFloat x = _mTransitionView.center.x;
if (x>=_centerMaxX) {
[self closeMenu];
}
}
@end
summary
The above is the imitation QQ sideslip menu function in IOS introduced by Xiaobian. I hope it will help you. If you have any questions, please leave me a message and Xiaobian will reply to you in time. Thank you very much for your support to the developeppaer website!