Add three View
viewcontroller.m//uisenior17_ Drawer effect////Created by lanou3g on 16/5/27.//copyright©2016 year Mingjie Zhang. All rights reserved.//#import "ViewController.h"//frame#define Xmgkeypath (OBJC, KeyPath) @ ((void) Objc.keypath, # KeyPath)//Gets the width of the screen # define SCREENW [UIScreen mainscreen].bounds.size.width//Gets the height of the screen # define SCREENH [UIScreen Mainscreen].bounds.size.height#define targetr 300#define targetl-200#define xmgmaxy 100@interface ViewController () @ Property (Strong, Nonatomic) Iboutlet UIView *mainv; @property (weak, nonatomic) Iboutlet UIView *leftv; @property (Weak, no natomic) Iboutlet UIView *ringhtv; @end @implementation viewcontroller-(void) viewdidload {[Super viewdidload]; Additional setup after loading the view, typically from a nib. [Self setupallchildview]; Add a drag gesture uipangesturerecognizer *pan = [[Uipangesturerecognizer alloc] initwithtarget:self action: @selector (pan:)]; [_mainv Addgesturerecognizer:pan]; KVO action: Always listen for changes to an object's properties//_main frAme Property Change//observer: Observer//keypath: Listener's property//nskeyvalueobservingoptionnew: Indicates that the listener is new and worthwhile to change [_MAINV addobserver:self ForK Eypath:xmgkeypath (_MAINV, frame) options: (nskeyvalueobservingoptionnew) Context:nil]; Add a tap to the view of the controller UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initwithtarget:self action: @selecto R (TAP)]; [Self.view Addgesturerecognizer:tap];} -(void) setupallchildview{//left UIView *LEFTV = [[UIView alloc] initWithFrame:self.view.bounds]; Leftv.backgroundcolor = [Uicolor Greencolor]; [Self.view ADDSUBVIEW:LEFTV]; _LEFTV = LEFTV; Right UIView *rightv = [[UIView alloc] initWithFrame:self.view.bounds]; Rightv.backgroundcolor = [Uicolor Bluecolor]; [Self.view Addsubview:rightv]; _ringhtv = Rightv; Main UIView *MAINV = [[UIView alloc] initWithFrame:self.view.bounds]; Mainv.backgroundcolor = [Uicolor Redcolor]; [Self.view ADDSUBVIEW:MAINV]; _MAINV = MainV;} Get the offset of the gesture-(void) pan: (Uipangesturerecognizer *) Pan {//Get the offset of the gesture cgpoint TRANSP = [Pan TRANSLATIONINVIEW:_MAINV] ; Gets the offset of the x-axis, relative to the last cgfloat OffsetX = transp.x; Modify the latest Main.frame _mainv.frame = [self framewithoffsetx:offsetx]; Reset [Pan Settranslation:cgpointzero INVIEW:_MAINV]; Judging the current finger is not lifted, indicating that the gesture ends if (pan.state = = uigesturerecognizerstateended) {//finger lift, positioning//x> half of the screen, positioning to a position on the right C Gfloat target = 0; if (_mainv.frame.origin.x > screenw*0.5) {target = TARGETR; } else if (Cgrectgetmaxx (_mainv.frame) < Screenw *0.5) {//MAX x< screen half time, define to the left of a position target = tar Getl; }//Get the X-axis offset CGFloat OffsetX = target-_mainv.frame.origin.x; [UIView animatewithduration:0.25 animations:^{_mainv.frame = [self framewithoffsetx:offsetx]; }]; }//the first main frame-(CGRect) Framewithoffsetx given an offset from the x-axis: (cgfloat) OffsetX {//Get the FRA of the current mainMe cgrect frame = _mainv.frame; Calculates the current x.y.w.h//Gets the latest x CGFloat x = frame.origin.x + OffsetX; Get the latest y cgfloat y = x/screenw *xmgmaxy; When the user moves to the left, the _main.x < 0,y need to be added as positive if (frame.origin.x < 0) {y = y; }//Get the latest h cgfloat h = screenH-2 *y; Get scaling cgfloat scale = h/screenh; Get the latest w cgfloat w = screenw *scale; return CGRectMake (x, Y, W, h); }//as long as the listener property changes, it will be called-(void) Observevalueforkeypath: (NSString *) KeyPath Ofobject: (ID) object change: (nsdictionary< NSString *,id> *) Change context: (void *) Context {if (_mainv.frame.origin.x > 0) {//swipe right, show left control, hide right control _ringhtv.hidden = YES; }else if (_mainv.frame.origin.x < 0) {_ringhtv.hidden = NO; }}-(void) Dealloc {//Remove observer [_MAINV removeobserver:self Forkeypath:xmgkeypath (_MAINV, frame)]; }-(void) Tap {if (_mainv.frame.origin.x! = 0) {//_MAINV restore the first position [UIView Animatewithduration:0.25 animations:^{_mainv.frame = self.view.bounds; }]; }} @end
As follows:
The normal screen
Swipe left
Swipe right
Animated drawer Effect