# Three Methods of left-sliding pop in IOS-navigation

Source: Internet
Author: User

# Three Methods of left-sliding pop in IOS-navigation
Three methods of left-sliding pop in IOS-navigation: pop Method

If we do not customize the back button in navigation, we can directly use the left-sliding pop method that comes with the system. However, if we customize the back button, we needself.navigationController.interactivePopGestureRecognizerThis attribute is set.

Key code
_ Weak typeof (self) weakSelf = self;
Self. navigationController. interactivepopgsturerecognizer. delegate =
WeakSelf;

The following is the instance code:

(Inheriting the AbeViewController class, you can use the pop method that comes with the system .)

# Import "AbeViewController. h" @ interface AbeViewController ()
  
   
@ End @ implementation AbeViewController-(void) viewDidLoad {[super viewDidLoad];}-(void) viewDidAppear :( BOOL) animated {// **************** method 1 ****************//// set sliding rollback _ weak typeof (self) weakSelf = self; self. navigationController. interactivepopgsturerecognizer. delegate = weakSelf; // determines whether it is the first view if (self. navigationController & [self. navigationController. viewControllers count] = 1) {self. navigationController. interactivepopgsturerecognizer. enabled = NO ;}} # pragma mark-UIGestureRecognizerDelegate // **************** method 1 ****************/ /-(BOOL) gestureRecognizerShouldBegin :( UIGestureRecognizer *) gestureRecognizer {return YES;} @ end
  
Custom left-sliding edge gesture Method

A gesture method is implemented, and pop is triggered when the gesture method is used.

The following is the instance code:

(Inheriting the AbeViewController class, you can use the pop method of the custom left-sliding edge gesture .)

# Import "AbeViewController. h" @ interface AbeViewController ()
  
   
@ End @ implementation AbeViewController-(void) viewDidLoad {[super viewDidLoad];}-(void) viewDidAppear :( BOOL) animated {// ************** method 2 ****************** // UIScreenEdgePanGestureRecognizer * edgePanGestureRecognizer = [[UIScreenEdgePanGestureRecognizer alloc] initWithTarget: self action: @ selector (edgePanGesture :)]; edgePanGestureRecognizer. delegate = self; edgePanGestureRecognizer. edges = UIRectEdgeLeft; [self. view addGestureRecognizer: edgePanGestureRecognizer];}-(void) didReceiveMemoryWarning {[super didreceivemorywarning];} # pragma mark-private method // ************** method 2 ***************** //-(void) edgePanGesture :( UIScreenEdgePanGestureRecognizer *) edgePanGestureRecognizer {[self. navigationController popViewControllerAnimated: YES];} # pragma mark-UIGestureRecognizerDelegate // **************** method 2 ****************/ /-(BOOL) gestureRecognizerShouldBegin :( UIGestureRecognizer *) gestureRecognizer {if (self. navigationController & [self. navigationController. viewControllers count] = 1) {return NO;} return YES;} @ end
  
Custom view left-shift pop

In the view, the pop method is triggered by moving left at any position.
In fact, a UIPanGestureRecognizer gesture is created and then triggered by the gesture.

Knowledge point:

[PanGestureRecognizer locationInView: XX] obtain the CGPoint of the pan gesture.
PanGestureRecognizer. state pan status
Self. interactivepopgsturerecognizer. enabled = NO; Native left slide is invalid

The following is the instance code:

(Inheriting the ABENavViewController class, you can use the pop method of the custom view left sliding gesture; ABENavViewController is the subclass of UINavigationController)

# Import "ABENavViewController. h "@ interface ABENavViewController () @ property (strong, nonatomic) Comment * comment; @ property (strong, nonatomic) UIImageView * backView; @ property (strong, nonatomic) NSMutableArray * backImgs; @ property (assign) CGPoint panBeginPoint; @ property (assign) CGPoint panEndPoint; @ end @ implementation ABENavViewController-(instancetype) initWithNibName :( NSString *) nibNameOrNil bundle :( NSBundle *) nibBundleOrNil {self = [super initWithNibName: nibNameOrNil bundle: nibBundleOrNil]; if (self) {// initlization} return self ;}- (void) loadView {[super loadView]; [self initilization];}-(void) viewDidLoad {[super viewDidLoad]; [self loadBaseUI];}-(void) initilization {self. backImgs = [[NSMutableArray alloc] init];}-(void) loadBaseUI {// The native method is invalid self. interactivepopgsturerecognizer. enabled = NO; // set the gesture self. panGestureRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget: self action: @ selector (panGestureRecognizerAction :)]; [self. view addGestureRecognizer: self. panGestureRecognizer];} # pragma mark-public method-(void) pushViewController :( UIViewController *) viewController animated :( BOOL) animated {// encrypt ([UIScreen mainScreen]. bounds. size, YES, 1.0); [[UIApplication sharedApplication]. keyWindow. layer renderInContext: UIGraphicsGetCurrentContext ()]; UIImage * img = UIGraphicsGetImageFromCurrentImageContext (); UIGraphicsEndImageContext (); [self. backImgs addObject: img]; [super pushViewController: viewController animated: animated];}-(UIViewController *) Updated :( BOOL) animated {[_ backImgs removeLastObject]; return [super ready: animated] ;}# pragma mark-private method-(void) panGestureRecognizerAction :( UIPanGestureRecognizer *) panGestureRecognizer {if ([self. viewControllers count] = 1) {return;} if (panGestureRecognizer. state = UIGestureRecognizerStateBegan) {NSLog (@ "slide start"); // stores the position self where the slide starts. panBeginPoint = [panGestureRecognizer locationInView: [UIApplication sharedApplication]. keyWindow]; // insert an image [self insertLastViewFromSuperView: self. view. superview];} else if (panGestureRecognizer. state = UIGestureRecognizerStateEnded) {NSLog (@ "sliding end"); // store data self. panEndPoint = [panGestureRecognizer locationInView: [UIApplication sharedApplication]. keyWindow]; if (_ panEndPoint. x-_ panBeginPoint. x)> 50) {[UIView animateWithDuration: 0.3 animations: ^ {[self moveNavigationViewWithLenght: [UIScreen mainScreen]. bounds. size. width];} completion: ^ (BOOL finished) {[self removeLastViewFromSuperView]; [self moveNavigationViewWithLenght: 0]; [self popViewControllerAnimated: NO];} else {[UIView animateWithDuration: 0.3 animations: ^ {[self failed: 0] ;}} else {// Add the moving effect CGFloat panLength = ([panGestureRecognizer locationInView: [UIApplication sharedApplication]. keyWindow]. x-_ panBeginPoint. x); if (panLength> 0) {[self moveNavigationViewWithLenght: panLength] ;}} /*** mobile view ** @ param lenght: length of movement */-(void) moveNavigationViewWithLenght :( CGFloat) lenght {// set the image position self. view. frame = CGRectMake (lenght, self. view. frame. origin. y, self. view. frame. size. width, self. view. frame. size. height); // dynamic shadow _ backView. alpha = (lenght/[UIScreen mainScreen]. bounds. size. width) * 2/3 + 0.33;}/*** superView of the top-level image in the illustration ** @ param superView image */-(void) insertLastViewFromSuperView :( UIView *) superView {// Insert the upper-level view background if (_ backView = nil) {_ backView = [[UIImageView alloc] initWithFrame: [UIScreen mainScreen]. bounds]; _ backView. image = [_ backImgs lastObject];} [self. view. superview insertSubview: _ backView belowSubview: self. view];}/*** remove the image of the previous level */-(void) removeLastViewFromSuperView {[_ backView removeFromSuperview]; _ backView = nil;} @ end

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.