Created
February 27, 2015 14:23
-
-
Save Air-Craft/be3438b76c011a1faf9b to your computer and use it in GitHub Desktop.
Present/dismiss a custom full screen modal alert #modals #ios #views #intermediate
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
static const CGFloat _BG_ALPHA = 0.3; | |
static const NSTimeInterval _ANIM_TIME = 0.3; | |
static UIWindow *_mainWindow; | |
static UIWindow *_alertWindow; | |
- (void)showExplainerScreen | |
{ | |
_mainWindow = [[UIApplication sharedApplication] keyWindow]; | |
// WINDOW | |
_alertWindow = [[UIWindow alloc] initWithFrame:_mainWindow.frame]; | |
_alertWindow.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight; | |
_alertWindow.opaque = NO; | |
_alertWindow.windowLevel = UIWindowLevelAlert; | |
// VC | |
UIViewController *vc = [[UIViewController alloc] init]; | |
_alertWindow.rootViewController = vc; | |
// If you want statusBar hidden you need a custom VC | |
// VIEW | |
// bg | |
vc.view.backgroundColor = UIColor.blackColor; | |
vc.view.frame = _alertWindow.frame; | |
// your custom views | |
// ... | |
// PRESENT | |
vc.view.alpha = 0; | |
[_alertWindow makeKeyAndVisible]; | |
[UIView | |
animateWithDuration:_ANIM_TIME | |
animations:^{ | |
vc.view.alpha = _BG_ALPHA; | |
} | |
completion:^(BOOL finished) { | |
}]; | |
// DISMISS ON TOUCH | |
UITapGestureRecognizer *gr = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismissExplainerScreen)]; | |
[vc.view addGestureRecognizer:gr]; | |
} | |
- (void)dismissExplainerScreen | |
{ | |
BOOL animated = YES; | |
void (^cleanup)(void) = ^void(void) | |
{ | |
[_mainWindow makeKeyAndVisible]; | |
_alertWindow = nil; | |
}; | |
if (!animated) | |
{ | |
cleanup(); | |
} | |
else | |
{ | |
[UIView | |
animateWithDuration:_ANIM_TIME | |
animations:^{ | |
_alertWindow.rootViewController.view.alpha = 0; | |
} | |
completion:^(BOOL finished) { | |
cleanup(); | |
}]; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment