Objective
The simple implementation of iOS drawer effect is now used in many applications, the Internet also has a lot of examples, this article is mainly through a number of simple code to achieve, there is a need to learn together.
Here is the effect chart
The sample code is as follows
#import <UIKit/UIKit.h>
@interface mainviewcontroller:uiviewcontroller
+ (instancetype) Mainviewcontrollerwithleftviewcontroller: (Uiviewcontroller *) Leftviewcontroller Withmainpageviewcontroller: ( Uiviewcontroller *) Mainpageviewcontroller;
@end
#import "MainViewController.h" #define Kwidth self.view.frame.size.width #define Kheight self.view.frame.size.height @
Interface Mainviewcontroller () @property (Nonatomic,strong) Uiviewcontroller *LEFTVC;
@property (Nonatomic,strong) Uiviewcontroller *centervc;
@property (nonatomic,assign) BOOL Isslider;
@property (Nonatomic,strong) UIView *corverview; @end @implementation Mainviewcontroller + (instancetype) Mainviewcontrollerwithleftviewcontroller: (Uiviewcontroller *) Leftviewcontroller Withmainpageviewcontroller: (Uiviewcontroller *) mainpageviewcontroller{MainViewController *
MAINVC = [[Mainviewcontroller alloc] init];
MAINVC.LEFTVC = Leftviewcontroller;
MAINVC.CENTERVC = Mainpageviewcontroller;
return MAINVC;
}-(void) viewdidload{[Super Viewdidload];
Self.isslider = NO;
Self.view.backgroundColor = [Uicolor Whitecolor];
[Self AddChildViewController:self.leftVC];
[Self.view AddSubview:self.leftVC.view];
[Self AddChildViewController:self.centerVC]; [Self.view AddSubview:self.centerVC.view];
Add gestures to the left and main view [self addgestureforview];
///Add a mask to the main view-(void) addcorverview{if (Self.corverview) {[Self.corverview Removefromsuperview];
Self.corverview = nil;
} Self.corverview = [[UIView alloc] initWithFrame:self.centerVC.view.bounds];
_corverview.backgroundcolor = [[Uicolor blackcolor] colorwithalphacomponent:0.0];
[Self.centerVC.view AddSubview:self.corverView];
//Remove main view cover-(void) removecoverview{if (Self.corverview) {[Self.corverview Removefromsuperview];
Self.corverview = nil; ///To add gestures to the left and main view-(void) addgestureforview{uiswipegesturerecognizer *rightgesture = [[Uiswipegesturerecognizer alloc
] initwithtarget:self action: @selector (swiperightaction:)];
Rightgesture.direction = Uiswipegesturerecognizerdirectionright;
[Self.centerVC.view Addgesturerecognizer:rightgesture]; Uiswipegesturerecognizer *leftgesture = [[Uiswipegesturerecognizer alloc] initwithtarget:self action: @selector (
Swipeleftaction:)]; Leftgesture.direction = Uiswipegesturerecognizerdirectionleft;
[Self.centerVC.view Addgesturerecognizer:leftgesture]; Uiswipegesturerecognizer *leftvcleftswipegesture = [[Uiswipegesturerecognizer alloc] initwithtarget:self action:@
Selector (leftvcleftswipeaction:)];
Leftvcleftswipegesture.direction = Uiswipegesturerecognizerdirectionleft;
[Self.leftVC.view Addgesturerecognizer:leftvcleftswipegesture];
}-(void) Swiperightaction: (ID) sender{[self MoveView:self.centerVC.view scale:0.8 panvalue:kwidth];
Self.isslider = YES;
[Self addcorverview];
}-(void) Swipeleftaction: (ID) sender{[self MoveView:self.centerVC.view scale:1 PANVALUE:KWIDTH/2];
Self.isslider = NO;
[Self removecoverview];
}-(void) Leftvcleftswipeaction: (ID) sender{[self MoveView:self.centerVC.view scale:1 PANVALUE:KWIDTH/2];
Self.isslider = NO;
[Self removecoverview]; //pan and Zoom one view-(void) Moveview: (UIView *) View scale: (cgfloat) scale panvalue: (cgfloat) value{[UIView Beginanimations:ni L ContExt:nil];
View.transform = Cgaffinetransformscale (Cgaffinetransformidentity,scale,scale);
View.center = Cgpointmake (value, VIEW.CENTER.Y);
[UIView commitanimations]; } @end
Summarize
The above is the entire content of this article, I hope the content of this article for everyone's study or work can help, if there is doubt you can message exchange.