I got this idea from https://gist.github.com/1279713
Preparation: In the xib modal view (or scene using the storyboard), I set up a full-screen UIImageView background (connect it to the .h file and give it the backgroundImageView property) with 0.3 alpha. And I set the background color of the view (UIView) as plain black.
Idea: Then, in the "viewDidLoad" of the modal view controller, I capture a screenshot from the initial state and set this image to the background of the UIImageView. Set the starting point Y to -480 and let it move to point Y 0 using a 0.4 second duration with the EaseInOut animation option. When we reject the view manager, just do the opposite.
Code for Modal View Controller Class
.h file:
@property (weak, nonatomic) IBOutlet UIImageView *backgroundImageView; - (void) backgroundInitialize; - (void) backgroundAnimateIn; - (void) backgroundAnimateOut;
.m file:
- (void) backgroundInitialize{ UIGraphicsBeginImageContextWithOptions(((UIViewController *)delegate).view.window.frame.size, YES, 0.0); [((UIViewController *)delegate).view.window.layer renderInContext:UIGraphicsGetCurrentContext()]; UIImage * screenshot = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); backgroundImageView.image=screenshot; } - (void) backgroundAnimateIn{ CGRect backgroundImageViewRect = backgroundImageView.frame; CGRect backgroundImageViewRectTemp = backgroundImageViewRect; backgroundImageViewRectTemp.origin.y=-480; backgroundImageView.frame=backgroundImageViewRectTemp; [UIView animateWithDuration:0.4 delay:0.0 options:UIViewAnimationCurveEaseInOut animations:^{ backgroundImageView.frame=backgroundImageViewRect; } completion:^(BOOL finished) { }]; } - (void) backgroundAnimateOut{ CGRect backgroundImageViewRect = backgroundImageView.frame; backgroundImageViewRect.origin.y-=480; [UIView animateWithDuration:0.4 delay:0.0 options:UIViewAnimationCurveEaseInOut animations:^{ backgroundImageView.frame=backgroundImageViewRect; } completion:^(BOOL finished) { }]; }
In viewDidLoad just call:
[self backgroundInitialize]; [self backgroundAnimateIn];
At any place where we reject the modal view controller, we call:
[self backgroundAnimateOut]
Please note that this will ALWAYS animate the background image. Therefore, if this modal presentation transition style (or segue transition style) is not set to "Cover Vertical", you may not need to invoke animation methods.
source share