Last active
August 29, 2015 14:21
-
-
Save D-32/c141c4a04ce72cc74237 to your computer and use it in GitHub Desktop.
Modal Window
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 CGFloat kHeight = 480; | |
@implementation ModalViewController { | |
UIView *_bg; | |
UIView *_container; | |
} | |
- (instancetype)init { | |
if (self = [super init]) { | |
self.providesPresentationContextTransitionStyle = YES; | |
self.definesPresentationContext = YES; | |
self.modalPresentationStyle = UIModalPresentationOverCurrentContext; | |
} | |
return self; | |
} | |
- (void)viewDidLoad { | |
[super viewDidLoad]; | |
_bg = [[UIView alloc] initWithFrame:self.view.bounds]; | |
_bg.backgroundColor = [UIColor clearColor]; | |
[_bg addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(close)]]; | |
[self.view addSubview:_bg]; | |
_container = [[UIView alloc] initWithFrame:CGRectMake(20, self.view.frame.size.height / 2, self.view.frame.size.width - 40, 0)]; | |
_container.backgroundColor = [UIColor whiteColor]; | |
_container.layer.cornerRadius = 6; | |
_container.clipsToBounds = YES; | |
[self.view addSubview:_container]; | |
} | |
- (void)viewWillAppear:(BOOL)animated { | |
[super viewWillAppear:animated]; | |
[UIView animateWithDuration:0.3 animations:^{ | |
_bg.backgroundColor = [UIColor colorWithWhite:0.0 alpha:0.7]; | |
CGRect f = _container.frame; | |
f.origin.y = self.view.frame.size.height / 2 - kHeight / 2; | |
f.size.height = kHeight; | |
_container.frame = f; | |
}]; | |
} | |
- (void)close { | |
[UIView animateWithDuration:0.3 animations:^{ | |
_bg.backgroundColor = [UIColor clearColor]; | |
CGRect f = _container.frame; | |
f.origin.y = self.view.frame.size.height; | |
_container.frame = f; | |
} completion:^(BOOL finished) { | |
[self dismissViewControllerAnimated:NO completion:nil]; | |
}]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment