Hmdrawviewcontroller.m
#import "HMDrawViewController.h"@interfaceHmdrawviewcontroller () @property (nonatomic, assign) BOOL isdraging;@end@implementationHmdrawviewcontroller- (void) viewdidload{//Uiviewcontroller[Super Viewdidload]; //Do any additional setup after loading the view. //1. Adding child controls[self addchildview];#warningThe third step is to observe the _mainview frame change//2. Monitoring /** * Add an observer to _mainview * * KeyPath: Listen to frame this property * Options: Monitor the change of the new value*/[_mainview addobserver:self Forkeypath:@"Frame"Options:nskeyvalueobservingoptionnew Context:nil]; }//when the frame property of _mainview is changed, it is called- (void) Observevalueforkeypath: (NSString *) KeyPath Ofobject: (ID)ObjectChange: (nsdictionary *) Change context: (void*) context{NSLog (@"%@", Nsstringfromcgrect (_mainview.frame)); if(_mainview.frame.origin.x <0) {//Move left//Show Right_rightview.hidden =NO; //Hide Left_leftview.hidden =YES; }Else if(_mainview.frame.origin.x >0){//Move Right//Show left_rightview.hidden =YES; //Hide Right_leftview.hidden =NO; }}#warningThe first step-(void) addchildview{// LeftUIView *leftview =[[UIView alloc] initWithFrame:self.view.bounds]; Leftview.backgroundcolor=[Uicolor Greencolor]; [Self.view Addsubview:leftview]; _leftview=Leftview; // RightUIView *rightview =[[UIView alloc] initWithFrame:self.view.bounds]; Rightview.backgroundcolor=[Uicolor Bluecolor]; [Self.view Addsubview:rightview]; _rightview=Rightview; //MainViewUIView *mainview =[[UIView alloc] initWithFrame:self.view.bounds]; Mainview.backgroundcolor=[Uicolor Redcolor]; [Self.view Addsubview:mainview]; _mainview=MainView;}#warningSecond Cloth-(void) touchesmoved: (Nsset *) touches withevent: (Uievent *)Event{ //Get Uitouch ObjectUitouch *touch =[touches anyobject]; //gets the current pointCgpoint Currentpoint =[Touch LocationInView:self.view]; //Get previous pointCgpoint Prepoint =[Touch PreviousLocationInView:self.view]; //X-axis offset: How much x is offset when the finger moves a little bitCGFloat OffsetX = currentpoint.x-Prepoint.x; //sets the frame of the current main view_mainview.frame =[self getcurrentframewithoffsetx:offsetx]; _isdraging=YES;}#warningFourth Step#defineHmmaxy 60//when the finger is shifted a little, the frame of the current main view is acreage from the offset of the x-axis-(CGRect) Getcurrentframewithoffsetx: (cgfloat) offsetx{cgfloat screenw=[UIScreen mainscreen].bounds.size.width; CGFloat screenh=[UIScreen mainscreen].bounds.size.height; //Gets the y-axis offset, and the y-axis offsets how much each finger movesCGFloat OffsetY = OffsetX * Hmmaxy/Screenw; CGFloat Scale= (Screenh-2* OffsetY)/screenh; if(_mainview.frame.origin.x <0) {//Swipe leftScale = (screenh +2* OffsetY)/screenh; } //get the previous frameCGRect frame =_mainview.frame; Frame.origin.x+=OffsetX; Frame.size.height= Frame.size.height *Scale ; Frame.size.width= Frame.size.width *Scale ; FRAME.ORIGIN.Y= (screenh-frame.size.height) *0.5; returnframe;}#defineHmrtarget 250#defineHMLTarget-220/*_mainview.frame.origin.x > Screenw * 0.5 positioning to the right Cgrectgetmaxx (_mainview.frame) < SCREENW * 0.5 positioning to the left -220 *///positioning- (void) touchesended: (Nsset *) touches withevent: (Uievent *)Event{ //Reset if(_isdraging = = NO && _mainview.frame.origin.x! =0) {[UIView animatewithduration:0.25animations:^{_mainview.frame=Self.view.bounds; }]; } cgfloat screenw=[UIScreen mainscreen].bounds.size.width; CGFloat Target=0; if(_mainview.frame.origin.x > Screenw *0.5) {//navigate to the righttarget =Hmrtarget; }Else if(Cgrectgetmaxx (_mainview.frame) < SCREENW *0.5) {//Navigate to lefttarget =Hmltarget; } [UIView animatewithduration:0.25animations:^{ if(target) {//If you need to locate the left or right//get x-axis offsetCGFloat OffsetX = target-_mainview.frame.origin.x; //sets the frame of the current main view_mainview.frame =[self getcurrentframewithoffsetx:offsetx]; }Else{//Restore_mainview.frame =Self.view.bounds; } }]; _isdraging=NO; }@end
HMDrawViewController.h file
#import <UIKit/UIKit.h>@interfacereadonly) UIView * ReadOnly) UIView *readonly) UIView *Rightview; @end
iOS 15th day (2, event handling, slide-off menu, drawer effect)