I am displaying a new UIWindow and I'm wanting it to slide in from the left and slide into the left side when it's dismissed. How can I animate the showing and removing of a UIWindow?
This is how I'm currently showing my new UIWindow.
- (void)showMenu
{
CGRect screenRect = [[UIScreen mainScreen] bounds];
CGFloat screenWidth = screenRect.size.width;
CGFloat screenHeight = screenRect.size.height;
UIButton *closeMenuButton = [UIButton buttonWithType:UIButtonTypeCustom];
[closeMenuButton setFrame:CGRectMake(250, 10, 50, 50)];
[closeMenuButton setImage:[UIImage imageNamed:@"close"] forState:UIControlStateNormal];
[closeMenuButton addTarget:self action:@selector(closeMenu) forControlEvents:UIControlEventTouchUpInside];
blurredView = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, screenWidth, screenHeight)];
[blurredView setBarStyle:UIBarStyleBlack];
MenuTableViewController *menu = [[MenuTableViewController alloc]initWithNibName:@"MenuTableViewController" bundle:nil];
menu.view.frame = CGRectMake(0, 30, screenWidth, screenHeight - 50);
menuWindow = [[UIWindow alloc]initWithFrame:[[UIScreen mainScreen]bounds]];
menuWindow.backgroundColor = [UIColor clearColor];
menuWindow.windowLevel = UIWindowLevelStatusBar;
menuWindow.rootViewController = menu;
[menuWindow addSubview:blurredView];
[blurredView addSubview:closeMenuButton];
[blurredView addSubview:menu.view];
[menuWindow makeKeyAndVisible];
}
Maybe Like this?
menuWindow = [[UIWindow alloc]initWithFrame:CGRectMake(0, 0, 0, screenHeight)];
menuWindow.backgroundColor = [UIColor clearColor];
menuWindow.windowLevel = UIWindowLevelStatusBar;
menuWindow.rootViewController = menu;
[menuWindow addSubview:blurredView];
[blurredView addSubview:closeMenuButton];
[blurredView addSubview:menu.view];
[menuWindow makeKeyAndVisible];
[UIView animateWithDuration:0.5 animations:^{
menuWindow.frame = screenRect;
blurredView.frame = screenRect;
menu.view.frame = CGRectMake(0, 30, screenWidth, screenHeight - 50);
}
completion:^(BOOL finished) {
}];
You can animate a UIWindow
like any other view (using UIView
animation methods).
A few things to remember. You need to retain a window for it to stay on screen. Also, you need to remember that windows live in screen coordinates, not in view coordinates, so you need to take into consideration the current interface orientation before starting an animation.
In the example above, I see you are creating a window, making it key and visible ... and nada. You need to retain the window.
Also, you are using the API incorrectly. Once you set a root view controller, the framework is responsible for the view. You add it as a subview again, and that can mess things. You really shouldn't add subviews to a window directly, just add them to the view controller's view hierarchy, and let that handle it.