2016-1-15 Drawer Effect Implementation Demo

Source: Internet
Author: User

////VIEWCONTROLLER.M//Drawer////Created by Mac on 16/1/15.//copyright©2016 year Mac. All rights reserved.//#import "ViewController.h"@interfaceViewcontroller () @property (nonatomic, weak) UIView*MainView, @property (nonatomic, weak) UIView*Leftview, @property (nonatomic, weak) UIView*Rightview; @property (nonatomic, assign) BOOL drag;@end@implementationViewcontroller- (void) viewdidload {[Super viewdidload]; CGFloat ScH=[UIScreen mainscreen].bounds.size.height; CGFloat ScW=[UIScreen mainscreen].bounds.size.width; UIView*leftview = [[UIView alloc] Initwithframe:cgrectmake (0,0, ScW, ScH)]; Leftview.backgroundcolor=[Uicolor Yellowcolor];    [Self.view Addsubview:leftview]; Self.leftview=Leftview; UIView*rightview = [[UIView alloc] Initwithframe:cgrectmake (0,0, ScW, ScH)]; Rightview.backgroundcolor=[Uicolor Redcolor];    [Self.view Addsubview:rightview]; Self.rightview=Rightview; UIView*mainview = [[UIView alloc] Initwithframe:cgrectmake (0,0, ScW, ScH)]; Mainview.backgroundcolor=[Uicolor Graycolor];    [Self.view Addsubview:mainview]; Self.mainview=MainView; Uipangesturerecognizer*pan =[[Uipangesturerecognizer alloc] initwithtarget:self action: @selector (pan:)];        [Self.mainview Addgesturerecognizer:pan]; UITapGestureRecognizer*tap =[[UITapGestureRecognizer alloc] initwithtarget:self action: @selector (tap:)]; [Self.view Addgesturerecognizer:tap];//[Self.rightview Addgesturerecognizer:tap];//[Self.leftview Addgesturerecognizer:tap];[Self.rightview Sethidden:yes];            [Self.leftview Sethidden:yes]; }#pragmaMark-event after View clicked-(voidTap: (UITapGestureRecognizer *) Tapgest {//UIView *view = Tapgest.view;NSLog (@"%s", __func__); NSLog (@"%LF", self.mainview.frame.origin.x); CGFloat ScH=[UIScreen mainscreen].bounds.size.height; CGFloat ScW=[UIScreen mainscreen].bounds.size.width; if(Self.mainview.frame.origin.x! =0) {[UIView animatewithduration:0.35animations:^{self.mainView.frame= CGRectMake (0,0, ScW, ScH); } Completion:^(BOOL finished) {[Self.rightview sethidden:yes];        [Self.leftview Sethidden:yes];    }]; }}- (void) Pan: (Uipangesturerecognizer *) pangest{cgfloat ScH=[UIScreen mainscreen].bounds.size.height; CGFloat ScW=[UIScreen mainscreen].bounds.size.width; Cgpoint Transform=[Pangest TranslationInView:panGest.view]; if(Transform.x >0) {//Swipe Right        if(Self.leftView.isHidden) {//determine the state of the start slip[Self.rightview Sethidden:no]; Self.drag=NO;         [Self SetupFrameWith:self.drag and:pangest]; }        if(Self.rightView.isHidden && self.mainview.frame.origin.x <=0){//indicates the user is now on the left side of the interface, but need to drag back, also need to pay attention to the size of the self.mainview.origin.x, in case the user dragged the boundsSelf.drag =YES;        [Self SetupFrameWith:self.drag and:pangest]; }            }Else if(Transform. x <0){//Swipe left        if(Self.rightView.isHidden) {[Self.leftview sethidden:no]; Self.drag=YES;        [Self SetupFrameWith:self.drag and:pangest]; }Else if(Self.leftview.ishidden&&self.mainview.frame.origin.x >=0){//now the background is Rightview, and the value of self.mianview.frame.origin.x needs to be judged in case the user drags over the boundsSelf.drag =NO;          [Self SetupFrameWith:self.drag and:pangest]; }    }    if(pangest.state = = uigesturerecognizerstateended) {//indicates end of gesture//Determine the current location of the main interface before deciding whether to hide the interface and bounce back//find the maximum minimum xCGFloat MaxX =Cgrectgetmaxx (self.mainView.frame); CGFloat MinX=Cgrectgetminx (self.mainView.frame); NSLog (@"%LF", MinX); //The user drags the old bounce back        if(MinX >0.8*ScW) {NSLog (@"%s", __func__); [UIView animatewithduration:0.2animations:^{self.mainView.frame= CGRectMake (0.8*SCW,0.4*SCW, ScW, ScH-0.8*ScW);        } ]; }        if(MaxX <0.2*ScW) {//NSLog (@ "%s", __func__);[UIView animatewithduration:0.2animations:^{self.mainView.frame= CGRectMake (-0.8*SCW,0.4*SCW, ScW, ScH-0.8*ScW);        } ]; }                                if(MinX < ScW/2&& MinX >0) {//at this point in the right interface and need to bounce back[UIView animatewithduration:0.35animations:^{self.mainView.frame= CGRectMake (0,0, ScW, ScH); } Completion:^(BOOL finished) {[Self.rightview sethidden:yes];//after the animation is finished hiding;         }]; }Else if(MaxX > ScW/2&& MinX <0) {[UIView animatewithduration:0.35animations:^{self.mainView.frame= CGRectMake (0,0, ScW, ScH); } Completion:^(BOOL finished) {[Self.leftview sethidden:yes];//after the animation is finished hiding;            }]; }                               }                }- (void) Setupframewith: (BOOL) Drag and: (Uipangesturerecognizer *) pangest{cgfloat ScH=[UIScreen mainscreen].bounds.size.height; CGFloat ScW=[UIScreen mainscreen].bounds.size.width; //Original FrameCGRect frame =Self.mainView.frame; Cgpoint Transform=[Pangest TranslationInView:panGest.view]; CGFloat x= frame.origin.x+transform.x; CGFloat y= FRAME.ORIGIN.Y + transform.x/2; if(Drag = =YES) {x= frame.origin.x+transform.x; Y= frame.origin.y-transform.x/2; } cgrect nextframe= CGRectMake (x, Y, ScW, sch-y*2); Self.mainView.frame=NextFrame;    [Pangest Settranslation:cgpointzero InView:panGest.view]; }@end

From this demo to learn to consolidate a lot of knowledge, feel good ~ ~

2016-1-15 Drawer Effect Implementation Demo

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.