Unwind with more than 1 segue using iOS 7 UIViewCo

2020-03-04 07:20发布

问题:

I am using the UIViewController animated transitions introduced in iOS7. I am able to produce some really great transitions going back and forth between VC's using segues etc.

Take a look at my picture below:

I can very easily go back and forth if it is just one segue/vc at a time. For example if, I go from Screen 1 to Screen 2, the animation works perfect. And then say I go back to 1 or forward to 3, it works perfectly.

But, if you notice on screen 4 at the bottom there is a button that says "Back To Screen 1"—this is an unwinding segue to screen 1. The issue is, I can't get the transition animation to work. In fact, the delegate methods never get called.

Here is how I have it set up (for example animating from screen 2 to 3):

//This is found in screen 2's view controller .m

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{

    if ([segue.identifier isEqualToString:@"screen2to3"]) {

        UIViewController *destination = segue.destinationViewController;
        destination.transitioningDelegate = self;
        destination.modalTransitionStyle = UIModalPresentationCustom;

    }
}

- (id<UIViewControllerAnimatedTransitioning>)animationControllerForPresentedController:(UIViewController *)presented presentingController:(UIViewController *)presenting sourceController:(UIViewController *)source {

    STSlideAnimation *animator = [STSlideAnimation new];
    animator.presenting = YES;
    return animator;
}

- (id<UIViewControllerAnimatedTransitioning>)animationControllerForDismissedController:(UIViewController *)dismissed {

    STSlideAnimation *animator = [STSlideAnimation new];
    return animator;
}

//This is the unwinding segue action

-(IBAction)returnToScreen2:(UIStoryboardSegue *)segue{

}

This works perfect going both to screen3 and back to screen2. The animation gets called both ways. However, I can't get any animation to work when going back more than one vc at a time. Any ideas? Let me know if I need to post more sample code.

回答1:

You need to make custom segue classes for each type of transition you wish to apply

#import <UIKit/UIKit.h>

@interface CustomSegue : UIStoryboardSegue

@end


@implementation CustomSegue

 -(void)perform {

 // write your transition code here
 // this code will be called every time transition is made

}

Select a segue in storybord - in inspector change its type to custom - select custom class you have made in above case "CustomSegue"

If you have subclassed it properly then you will find inspector as below

Unwind as usual but will get the same effect even if you have poped from 4th controller to 1st controller