Gesture swipe back function for IOS 7

Source: Internet
Author: User

The iOS app created with the default template now supports the gesture return function, which is invalid if the back button of the navigation bar is customized, or if the manual setting is not available here.

if ([Self.navigationcontroller respondstoselector: @selector (Interactivepopgesturerecognizer)]) {      self.navigationController.interactivePopGestureRecognizer.enabled = NO;  }  

If the gesture return fails because of a custom navigation button, you can add the following code to the Navigationcontroller viewdidload function:

-(void) viewdidload  {      [super viewdidload];      Do any additional setup after loading the view.            __weak typeof (self) weakself = self;      if ([Self respondstoselector: @selector (Interactivepopgesturerecognizer)]) {          Self.interactivePopGestureRecognizer.delegate = weakself;      }  }  

This will allow you to swipe back to the previous layer by gesture, but if the gesture is triggered during push, the navigation bar will crash (as you can see from the log). For this problem, we need to disable the gesture swipe return function in the pop process, need to implement the Protocol "uinavigationcontrollerdelegate"

-(void) Pushviewcontroller: (Uiviewcontroller *) Viewcontroller animated: (BOOL) animated  {        //fix ' nested pop Animation can result in corrupted navigation bar '      if ([Self respondstoselector: @selector ( Interactivepopgesturerecognizer)] {          self.interactivePopGestureRecognizer.enabled = NO;      }            [Super Pushviewcontroller:viewcontroller animated:animated];  } -(void) Navigationcontroller: (Uinavigationcontroller *) Navigationcontroller         Didshowviewcontroller: ( Uiviewcontroller *) Viewcontroller                      animated: (BOOL) animated  {      if ([Navigationcontroller Respondstoselector: @selector (Interactivepopgesturerecognizer)]) {          navigationController.interactivePopGestureRecognizer.enabled = YES;      }  }  

In addition to using the system default animations, you can also use custom transition animations (plump documents):

-(id<uiviewcontrolleranimatedtransitioning>) Navigationcontroller: (Uinavigationcontroller *) Navigationcontroller animationcontrollerforoperation: (uinavigationcontrolleroperation) O                                                   Peration Fromviewcontroller: (Uiviewcontroller *) FROMVC Toviewcontroller: (Uiviewcontroller *) ToVC {if (Operation = = Uinavigationcontrollerop          Erationpop) {if (Self.popanimator = = nil) {self.popanimator = [wqpopanimator new];      } return self.popanimator;  } return nil; }-(Id<uiviewcontrollerinteractivetransitioning>) Navigationcontroller: (Uinavigationcontroller *) Navigationcontroller Interactioncontrollerforanimationcontroller: (id<uiviewcontrolleranimated  transitioning>) Animationcontroller {return self.popinteractioncontroller; } #pragma mark--(void) EnablepaNtopopfornavigationcontroller: (Uinavigationcontroller *) Navigationcontroller {Uiscreenedgepangesturerecognizer * Left2rightswipe = [[Uiscreenedgepangesturerecognizer alloc] Init      Withtarget:self Action: @selector (Didpantopop:)];      [Left2rightswipe setdelegate:self];      [Left2rightswipe Setedges:uirectedgeleft];            [Navigationcontroller.view Addgesturerecognizer:left2rightswipe];      Self.popanimator = [Wqpopanimator new];  Self.supportpan2pop = YES;            }-(void) Didpantopop: (Uipangesturerecognizer *) pangesture {if (!self.supportpan2pop) return;            UIView *view = Self.navigationController.view; if (pangesture.state = = Uigesturerecognizerstatebegan) {Self.popinteractioncontroller = [uipercentdriveninteract          Ivetransition new];      [Self.navigationcontroller Popviewcontrolleranimated:yes]; } else if (pangesTure.state = = uigesturerecognizerstatechanged) {cgpoint translation = [Pangesture Translationinview:view];          CGFloat d = fabs (Translation.x/cgrectgetwidth (view.bounds));      [Self.popinteractioncontroller Updateinteractivetransition:d];              } else if (pangesture.state = = uigesturerecognizerstateended) {if ([pangesture velocityinview:view].x > 0) {          [Self.popinteractioncontroller finishinteractivetransition];          } else {[Self.popinteractioncontroller cancelinteractivetransition];      } Self.popinteractioncontroller = nil;   }  }

The following proxy method is used to provide a non-interactive transition animation:

-(id<uiviewcontrolleranimatedtransitioning>) Navigationcontroller: (Uinavigationcontroller *) Navigationcontroller                                    animationcontrollerforoperation: (uinavigationcontrolleroperation) Operation                                                 Fromviewcontroller: (Uiviewcontroller *) Fromvc                                                   toviewcontroller: (Uiviewcontroller *) ToVC  {      if ( Operation = = Uinavigationcontrolleroperationpop) {          if (self.popanimator = = nil) {              self.popanimator = [ Wqpopanimator new];          }          return self.popanimator;      }            return nil;  }  

The following proxy method provides an interactive animation:

-(id<uiviewcontrollerinteractivetransitioning>) Navigationcontroller: (Uinavigationcontroller *) Navigationcontroller                           Interactioncontrollerforanimationcontroller: (id< uiviewcontrolleranimatedtransitioning>) Animationcontroller  {      return self.popinteractioncontroller;  }  

These two are used together. First, we need to have an animation:

@interface wqpopanimator:nsobject <UIViewControllerAnimatedTransitioning>    @end  

  

#import "WQPopAnimator.h" @implementation Wqpopanimator-(Nstimeinterval) Transitionduration: (ID <uiviewcontrolle  rcontexttransitioning>) Transitioncontext {return 0.4; }-(void) Animatetransition: (id<uiviewcontrollercontexttransitioning>) Transitioncontext {UIViewController      *toviewcontroller = [Transitioncontext Viewcontrollerforkey:uitransitioncontexttoviewcontrollerkey]; Uiviewcontroller *fromviewcontroller = [Transitioncontext viewcontrollerforkey:      Uitransitioncontextfromviewcontrollerkey];            [[Transitioncontext Containerview] InsertSubview:toViewController.view BelowSubview:fromViewController.view];      __block CGRect torect = toViewController.view.frame;      CGFloat Originx = torect.origin.x;      Torect.origin.x-= TORECT.SIZE.WIDTH/3;            ToViewController.view.frame = Torect; [UIView animatewithduration:[self Transitionduration:transitioncontext] animations:^{cgrect fromRect = FromViewC Ontroller.view.frame;          Fromrect.origin.x + = FromRect.size.width;                    FromViewController.view.frame = Fromrect;          torect.origin.x = Originx;      ToViewController.view.frame = Torect; } completion:^ (BOOL finished) {[Transitioncontext completetransition:![      Transitioncontext Transitionwascancelled]];  }];   } @end

Second, interactive animations are

Uipercentdriveninteractivetransition

To be maintained and updated during the sliding process according to the sliding distance:

} else if (pangesture.state = = uigesturerecognizerstatechanged) {          cgpoint translation = [pangesture Translationinview:view];          CGFloat d = fabs (Translation.x/cgrectgetwidth (view.bounds));          [Self.popinteractioncontroller Updateinteractivetransition:d];  

When the gesture ends, make a final move:

} else if (pangesture.state = = uigesturerecognizerstateended) {          if ([Pangesture velocityinview:view].x > 0) {              [Self.popinteractioncontroller finishinteractivetransition];          } else {              [Self.popinteractioncontroller cancelinteractivetransition];          }          Self.popinteractioncontroller = nil;      }  

Similarly, a custom animation will have the above mentioned navigation bar crash problem, can also be resolved by a similar method:

-(void) Navigationcontroller: (Uinavigationcontroller *) Navigationcontroller         Didshowviewcontroller: ( Uiviewcontroller *) Viewcontroller                      animated: (BOOL) animated  {      if (Viewcontroller = = Self.navigationController.pushingViewController) {          self.supportpan2pop = YES;          Self.navigationController.pushingViewController = nil;      }  

Add: The gesture agent needs to be set at the first ([0]) Viewcontroller of the current Navgationcontroller, and will not respond.

Original address: http://blog.csdn.net/jasonblog/article/details/28282147

  

Gesture swipe back function for IOS 7

Related Article

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.