Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make a popup/dialog view in iOS in a separate ViewController?

I'm making an iOS game and I want to achieve this effect: pause popup 2 different controllers

I know how to do it using one view controller but my view controller class is already bloated with code and the storyboard of that screen is a mess (I actually have many more views that the image above, I just hid them for clarity reasons).

So my questions is: Is there a way to achieve this by using a separate view controller to manage this pause popover? I need a segue that will not dismiss the previous presented screen. I want to do that using storyboards. I tried "popover" segue but it appeared with a weird border that I don't want to.

Also I need that all controls that are not managed by pause view controller stop listening to events. I thought of using a black transparent view that covers the entire screen as BG for the pause view, and make it ignore events. Is there a better approach?

Thanks in advance.

like image 343
Felipe Lira Avatar asked Apr 26 '13 18:04

Felipe Lira


1 Answers

If you want to achieve it using storyboard, it's quite simple.

I attach example code

1 - Create your custom PauseViewController in Storyboard. Use a fullscreen view, black color, with Alpha 0 and a subview containing your pause controls. Create the coresponding .h and .m files

2 - add and link your controls (UIButtons). Add a dismiss method like this. This method will remove the view from the hierarchy and make it not visible changing the alpha on dismiss.

- (IBAction)dismiss:(id)sender {
    [UIView animateWithDuration:0.3f animations:^{
        self.view.alpha = 0.0f;
    } completion:^(BOOL finished) {
        [self.view removeFromSuperview];
        [self removeFromParentViewController];
    }];
}

3 - subclass UIStoryboardSegue creating your own segue class. Then, override the perform method with something like this. This simply takes the two controllers (source and destinations) and put the destination view over the source view with an animation.

-(void)perform{
    UIViewController *dst = [self destinationViewController];
    UIViewController *src = [self sourceViewController];

    // add the view to the hierarchy and bring to front
    [src addChildViewController:dst];
    [src.view addSubview:dst.view];
    [src.view bringSubviewToFront:dst.view];

    // set the view frame
    CGRect frame;
    frame.size.height = src.view.frame.size.height;
    frame.size.width = src.view.frame.size.width;
    frame.origin.x = src.view.bounds.origin.x;
    frame.origin.y = src.view.bounds.origin.y;
    dst.view.frame = frame;

    [UIView animateWithDuration:0.3f animations:^{ 
        dst.view.alpha = 0.5f;
    }];
}

4 - link your segue, choose custom and insert the custom segue class name.

Ta-daaaa!

If you need other details check the example code or add a comment.

like image 151
LombaX Avatar answered Nov 14 '22 23:11

LombaX