简体   繁体   中英

Pan gesture used for popping view controller crashing on iOS 7

I have a pan gesture that I have added on my app's root view controller on the 1st level to let the user swipe anywhere to go to the previous level in the navigation stack (the gesture is not on the 0 level to prevent going into a black hole :) ). This has worked and still works perfectly fine on iOS versions prior to 7.

When I try to swipe back using iOS 7, I get these messages in the console: [11050:a0b] nested pop animation can result in corrupted navigation bar [11050:a0b] Finishing up a navigation transition in an unexpected state. Navigation Bar subview tree might get corrupted.

At this point, the app hasn't crashed.

Then when I try to swipe back one more time to the previous level, it crashes. I'm just trying to figure out why this bug has risen in iOS 7, and how I can fix it. Any help is duly and greatly appreciated!

Here is my code:

RootViewController.m:

- (void)viewDidLoad
{
    [super viewDidLoad];
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(pop:)];

    pan.delegate = self;

    [self.view addGestureRecognizer:pan];
}

- (void)pop:(UIPanGestureRecognizer*)pan
{
    if (pan.state == UIGestureRecognizerStateBegan || pan.state == UIGestureRecognizerStateChanged)
    {
        CGPoint vel = [pan velocityInView:self.view];

        if (vel.x > 1000)
        {
            [self.navigationController popViewControllerAnimated:YES];
        }
    }
}

I suspect the problem is your state condition. By testing for UIGestureRecognizerStateBegan and UIGestureRecognizerStateChanged, the delegate is triggering many pops (which are animated, so the next pop is triggered while the previous one is still animating).

Instead, test if (pan.state == UIGestureRecognizerStateEnded) to get just the one you need.

Also, don't worry about popping into nowhere when the NavVC is already at root. It's smart enough not to pop the root.

(As to why this is misbehaving in os7 only, I can only speculate that < ios7 logic removes the gesture recognizers at an earlier stage in the pop process).

If there is a back button visible for the current controller in the UINavigationController's stack, the user will be able to swipe right from the left edge of the screen to go back. You may want to consider not adding the pan gesture recognizer if the app is running on iOS7.

You can check the iOS version using [UIDevice currentDevice].systemVersion .

I realize your question stated that you added the gesture recognizer to allow the user to swipe anywhere to go back, but (hopefully) iOS7 users will quickly become familiar with the swipe from the left edge of the screen.

当速度达到1000时发生的事情,每次调用函数时都会弹出viewcontroller,当它的动画为YES时,它会在弹出viewcontroller之前多次被调用,我认为它会让你崩溃。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM