Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you perform a customized segue (modal segue) from a UIButton in storyboard

I would like to segue a destination scene through a button present in source scene. I also would like to have a control of the transition animation between the viewcontroller (I want to animate the 2 views from right to left). Is it possible to do so through the replace segue? I try both the replace segue and the push segue but the segue is not happening any suggestion how i should proceed there? Thanks!

like image 757
tiguero Avatar asked Feb 22 '23 07:02

tiguero


1 Answers

I found out that the replace segue and push segue were misleading because the replace seems to be available for a master detail controller and the push segue for a navigation controller only. In this case I needed to implement a customize segue instead. You need to subclass the UIStoryboardSegue and override the perform segue.

Here is an e.g of my code:

-(void)perform{
    UIView *sourceView = [[self sourceViewController] view];
    UIView *destinationView = [[self destinationViewController] view];      

    UIImageView *sourceImageView;
    sourceImageView = [[UIImageView alloc] 
                       initWithImage:[sourceView pw_imageSnapshot]];

    // force the destination to be in landscape before screenshot
    destinationView.frame = CGRectMake(0, 0, 1024, 748);
    CGRect originalFrame = destinationView.frame;
    CGRect offsetFrame = CGRectOffset(originalFrame, originalFrame.size.width, 0);


    UIImageView *destinationImageView;
    destinationImageView = [[UIImageView alloc] 
                            initWithImage:[destinationView pw_imageSnapshot]];

    destinationImageView.frame = offsetFrame;  
    [self.sourceViewController presentModalViewController:self.destinationViewController animated:NO];

    [destinationView addSubview:sourceImageView];                        
    [destinationView addSubview:destinationImageView]; 

    void (^animations)(void) = ^ {                                     
        [destinationImageView setFrame:originalFrame];

    };

    void (^completion)(BOOL) = ^(BOOL finished) {                       
        if (finished) {

            [sourceImageView removeFromSuperview];
            [destinationImageView removeFromSuperview];

        }
    };

    [UIView animateWithDuration:kAnimationDuration delay:.0 options:UIViewAnimationOptionCurveEaseOut animations:animations completion:completion];
 }

The main idea is to create a screenshot view of the source and destination scene; add them to the destination scene view, control the animation of those two views, call to the presentModalViewController function on the sourceviewController and remove the two screenshots views when done with the animation.

One can found an example of implementing a screenshot utility function here in Ch15 of this link: http://learnipadprogramming.com/source-code/

like image 150
tiguero Avatar answered Apr 29 '23 18:04

tiguero