8 lines of code teaches you to handle the navigation controller full screen slide back effect

Source: Internet
Author: User

@ acridine A Zheng authorized this site reprint

Preface

This article, is about the navigation controller full-screen slide return effect, and the code is very small, 10 lines within.

Effect

If you like my article, you can follow me, you can also come to small brother, learn about our iOS training courses. In succession there will be updated ing ....

One, custom navigation controller

Purpose: Use your own defined navigation controller in the future when you need to use the full-screen swipe back function.

second, analysis of navigation controller slide -off function

Effect: The navigation controller comes with a slide-slip function By default, and when the user slides to the left of the interface, there is a slide-off function.

The system comes with a side-slip effect:

Analysis:

1. The navigation controller's view comes with a swipe gesture, except that the gesture's trigger range is only on the left.

2. When the user drags on the left side of the interface, it will trigger the swipe gesture method, and has the sliding return function, indicating that the system gesture triggering method has realized the sliding return function.

3. Why is the system gesture triggering method already implemented the sliding return function?

Reason:

    • When you create a sliding gesture object, you need to bind the listener and invoke the target action when the gesture is triggered.

1
    • When the user slides to the left of the interface, there is a sliding return function, because the trigger gesture, call the target action method, the action method to implement the sliding return function, otherwise there will be no sliding return effect.

three, realize full-screen slide function analysis

Print the navigation controller's own swipe gesture to see its true face.

The system comes with a sliding gesture interactivepopgesturerecognizer

1 2 3 4 5
//  self pointing to the navigation controller, the Viewdidload method in the navigation controller prints-  (void) viewdidload {    [ super viewdidload];     nslog (@ "%@", Self.interactivepopgesturerecognizer); }

Print result Picture:

From the figure:

1. The system comes with gestures that are Uiscreenedgepangesturerecognizer type objects, screen edge swipe gestures

2. System comes with gesture target is an object of type _uinavigationinteractivetransition

The action method called by 3.target is called Handlenavigationtransition:

Analysis:

Uiscreenedgepangesturerecognizer, look at the name to know that the scope of this gesture only in the periphery of the screen, because of this gesture, the system comes with the sliding effect, can only achieve side sliding.

Iv. How to realize full-screen sliding function

To your own navigation controller, add a full-screen swipe gesture, call the system with the swipe gesture of the target action method, using the system to implement the sliding return function, plus their full-screen swipe gesture, there will be full-screen sliding function.

Question: How do I get the target object that comes with the system?, the action method name already knows, and the system is definitely implemented in the target object, as long as the target object, call this method is OK.

through the printing system with the swipe gesture of the agent, found exactly the _uinavigationinteractivetransition object, so I guess this proxy object is the target object, as long as it gets it, the system comes with a sliding gesture target object.

1 2

To print a picture:

Navigation Controller Full Screen swipe note points:

1. Disable the system from using sliding gestures.

2. Only the non-root controller of the navigation controller needs to trigger gestures, use gesture proxies, and control gesture triggering.

Full-screen Slide code implementation

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 -  (void) viewdidload {    [super viewDidLoad];     //   Get the target object with sliding gestures from the system     id target =  Self.interactivePopGestureRecognizer.delegate;     //  Create a full-screen swipe gesture that calls the system's action method with the target of the swipe gesture      Uipangesturerecognizer *pan = [[uipangesturerecognizer alloc] initwithtarget:target  action: @selector (handlenavigationtransition:)];     //  set gesture proxy, intercept gesture trigger     pan.delegate = self;     //  Add a full-screen swipe gesture to the view of the navigation controller     [self.view  Addgesturerecognizer:pan];     //  Disable the use of the system's own sliding gestures      self.interactivepopgesturerecognizer.enabled = no; //  When to call: The agent will be asked before each trigger gesture, whether it is triggered.   Action: Intercept gesture Trigger-  (BOOL) Gesturerecognizershouldbegin: (uigesturerecognizer *) Gesturerecognizer {     //  Note: Only the non-root controller has the sliding return function, the root controller does not.     //  determine if the navigation controller has only one sub-controller, if there is only one child controller, it must be the root controller     if  ( self.childviewcontrollers.count == 1)  {        //  Indicates that the user does not need to trigger a swipe gesture on the root controller interface,         return NO;     }     return YES; }

Contact Information

If you like this article, you can continue to follow me, Weibo: Acridine a Zheng, welcome to communicate.

(PS: In addition, our company small code elder brother, invites it to have ambition, has the ability, has the vigor, has the drive each road hero joins together the venture, the detail may click Small Yards elder brother, small size elder brother official Micro Bo, or micro Bo privately chats me)

8 lines of code teaches you to handle the navigation controller full screen slide back effect

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.