Search code examples
iosxcodetransitionseguecompletion-block

Custom segue transition with completion block & properties


I want to make a custom transition between view controllers, a slide from left to right like the iOS Basic Transition Effect between pages in the AppLaucher, but with a darkening.

I have a custom segue between viewControllers (let's call them FirstVC and SecondVC). For the darkening I have a black view which fade in during the animation (the SecondVC is darker than the FirstVC). I have a class - subclass of UIStoryboardSegue - for my custom segue, with the -(void)perform method implemented. In this method, I have implemented (with more details) :

//...Initials frames

UIView *blackView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 568)];
blackView.backgroundColor = [UIColor blackColor];
blackView.alpha = 0;
[UIView animateWithDuration:0.3 animations:^{
blackView.alpha = 0.5;
//...Animations
}
completion:^(BOOL finished){

}];

I precise that the two View Controllers are in two different Navigation View Controllers.

For the darkening, I would like to modify the "darkView" property declared in the SecondVC class when the transition is finished, to store the darkening after the transition.

For this, I would like to do one of the following options (if one of those works I don't need the others) :

  1. Access to the darkView property of the SecondVC directly in the completion block and modify it

  2. Detect when the animation is finished and modify the property at that point, in a method implemented in the SecondVC class

  3. Write [dest.view addSubview:whiteView]; in the completion block ("dest" being the destination view controller), and access to THIS subview in an other method of SecondVC

I've already tried a lot of things for the three solutions, but I'd really appreciate your help. Thanks


Solution

  • Well If you have a custom segue class implemented , then getting an instance of the secondViewController thats about to get pushed on the navigation stack should be simple.

    Try this in your perform method

    FirstViewController *firstVC = self.sourceViewController;
    SecondViewController *secondVC = self.destinationViewController;
    

    Probably you can declare a public UIView Property on your secondVC and access it and also probably expose a method and call it once the animation completes.