2014-09-23 02:32:31 +00:00
|
|
|
/**
|
2014-09-24 05:07:02 +00:00
|
|
|
* Copyright Maarten Billemont (http://www.lhunath.com, lhunath@lyndir.com)
|
|
|
|
*
|
|
|
|
* See the enclosed file LICENSE for license information (LGPLv3). If you did
|
|
|
|
* not receive this file, see http://www.gnu.org/licenses/lgpl-3.0.txt
|
|
|
|
*
|
|
|
|
* @author Maarten Billemont <lhunath@lyndir.com>
|
|
|
|
* @license http://www.gnu.org/licenses/lgpl-3.0.txt
|
|
|
|
*/
|
2014-09-23 02:32:31 +00:00
|
|
|
|
|
|
|
//
|
|
|
|
// MPOverlayViewController.h
|
|
|
|
// MPOverlayViewController
|
|
|
|
//
|
|
|
|
// Created by lhunath on 2014-09-22.
|
|
|
|
// Copyright, lhunath (Maarten Billemont) 2014. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
#import "MPOverlayViewController.h"
|
|
|
|
|
2014-09-24 05:07:02 +00:00
|
|
|
@implementation MPOverlayViewController {
|
|
|
|
NSMutableDictionary *_dismissSegueByButton;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)awakeFromNib {
|
|
|
|
|
|
|
|
[super awakeFromNib];
|
2014-09-23 02:32:31 +00:00
|
|
|
|
2014-09-24 05:07:02 +00:00
|
|
|
_dismissSegueByButton = [NSMutableDictionary dictionary];
|
|
|
|
}
|
2014-09-23 02:32:31 +00:00
|
|
|
|
2014-09-26 04:32:07 +00:00
|
|
|
- (void)viewDidLoad {
|
2014-09-23 02:32:31 +00:00
|
|
|
|
2014-09-26 04:32:07 +00:00
|
|
|
[super viewDidLoad];
|
2014-09-23 02:32:31 +00:00
|
|
|
|
2014-09-26 04:32:07 +00:00
|
|
|
[self performSegueWithIdentifier:@"root" sender:self];
|
2014-09-23 02:32:31 +00:00
|
|
|
}
|
|
|
|
|
2014-09-24 05:07:02 +00:00
|
|
|
- (UIViewController *)childViewControllerForStatusBarStyle {
|
|
|
|
|
|
|
|
return [self.childViewControllers lastObject];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (UIViewController *)childViewControllerForStatusBarHidden {
|
|
|
|
|
|
|
|
return self.childViewControllerForStatusBarStyle;
|
|
|
|
}
|
|
|
|
|
2014-09-23 02:32:31 +00:00
|
|
|
- (UIStoryboardSegue *)segueForUnwindingToViewController:(UIViewController *)toViewController
|
|
|
|
fromViewController:(UIViewController *)fromViewController identifier:(NSString *)identifier {
|
|
|
|
|
|
|
|
return [[MPOverlaySegue alloc] initWithIdentifier:identifier source:fromViewController destination:toViewController];
|
|
|
|
}
|
|
|
|
|
2014-09-27 05:27:05 +00:00
|
|
|
- (UIView *)addDismissButtonForSegue:(MPOverlaySegue *)segue {
|
2014-09-24 05:07:02 +00:00
|
|
|
|
|
|
|
UIButton *dismissButton = [UIButton buttonWithType:UIButtonTypeCustom];
|
|
|
|
[dismissButton addTarget:self action:@selector( dismissOverlay: ) forControlEvents:UIControlEventTouchUpInside];
|
2014-09-27 05:27:05 +00:00
|
|
|
dismissButton.backgroundColor = [UIColor colorWithWhite:0 alpha:0.5f];
|
|
|
|
dismissButton.alpha = 0;
|
2014-09-24 05:07:02 +00:00
|
|
|
dismissButton.frame = self.view.bounds;
|
|
|
|
dismissButton.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
|
|
|
|
_dismissSegueByButton[[NSValue valueWithNonretainedObject:dismissButton]] =
|
|
|
|
[[MPOverlaySegue alloc] initWithIdentifier:@"dismiss-overlay"
|
|
|
|
source:segue.destinationViewController destination:segue.sourceViewController];
|
2014-09-27 05:27:05 +00:00
|
|
|
|
2014-09-24 05:07:02 +00:00
|
|
|
[self.view addSubview:dismissButton];
|
2014-09-27 05:27:05 +00:00
|
|
|
return dismissButton;
|
2014-09-24 05:07:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)dismissOverlay:(UIButton *)dismissButton {
|
|
|
|
|
|
|
|
NSValue *dismissSegueKey = [NSValue valueWithNonretainedObject:dismissButton];
|
|
|
|
[((UIStoryboardSegue *)_dismissSegueByButton[dismissSegueKey]) perform];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)removeDismissButtonForViewController:(UIViewController *)viewController {
|
|
|
|
|
|
|
|
UIButton *dismissButton = nil;
|
|
|
|
for (NSValue *dismissButtonValue in [_dismissSegueByButton allKeys])
|
|
|
|
if (((UIStoryboardSegue *)_dismissSegueByButton[dismissButtonValue]).sourceViewController == viewController) {
|
|
|
|
dismissButton = [dismissButtonValue nonretainedObjectValue];
|
|
|
|
NSAssert([self.view.subviews containsObject:dismissButton], @"Missing dismiss button in dictionary.");
|
|
|
|
}
|
|
|
|
if (!dismissButton)
|
|
|
|
return;
|
|
|
|
|
|
|
|
NSValue *dismissSegueKey = [NSValue valueWithNonretainedObject:dismissButton];
|
|
|
|
[_dismissSegueByButton removeObjectForKey:dismissSegueKey];
|
|
|
|
|
|
|
|
[UIView animateWithDuration:0.1f animations:^{
|
|
|
|
dismissButton.alpha = 0;
|
|
|
|
} completion:^(BOOL finished) {
|
|
|
|
[dismissButton removeFromSuperview];
|
|
|
|
}];
|
|
|
|
}
|
|
|
|
|
2014-09-23 02:32:31 +00:00
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation MPOverlaySegue
|
|
|
|
|
2014-09-27 05:27:05 +00:00
|
|
|
+ (instancetype)dismissViewController:(UIViewController *)viewController {
|
|
|
|
|
|
|
|
return [[self alloc] initWithIdentifier:nil source:viewController destination:viewController];
|
|
|
|
}
|
|
|
|
|
2014-09-23 02:32:31 +00:00
|
|
|
- (void)perform {
|
|
|
|
|
|
|
|
UIViewController *sourceViewController = self.sourceViewController;
|
|
|
|
UIViewController *destinationViewController = self.destinationViewController;
|
|
|
|
MPOverlayViewController *containerViewController = self.sourceViewController;
|
2014-09-24 05:07:02 +00:00
|
|
|
while (containerViewController && ![(id)containerViewController isKindOfClass:[MPOverlayViewController class]])
|
2014-09-23 02:32:31 +00:00
|
|
|
containerViewController = (id)containerViewController.parentViewController;
|
|
|
|
NSAssert( [containerViewController isKindOfClass:[MPOverlayViewController class]],
|
|
|
|
@"Not an overlay container: %@", containerViewController );
|
|
|
|
|
|
|
|
if (!destinationViewController.parentViewController) {
|
|
|
|
// Winding
|
|
|
|
[containerViewController addChildViewController:destinationViewController];
|
2014-09-27 05:27:05 +00:00
|
|
|
UIView *dismissButton = [containerViewController addDismissButtonForSegue:self];
|
2014-09-24 05:07:02 +00:00
|
|
|
|
|
|
|
destinationViewController.view.frame = containerViewController.view.bounds;
|
|
|
|
destinationViewController.view.translatesAutoresizingMaskIntoConstraints = YES;
|
|
|
|
destinationViewController.view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
|
2014-09-23 02:32:31 +00:00
|
|
|
[containerViewController.view addSubview:destinationViewController.view];
|
2014-09-26 04:32:07 +00:00
|
|
|
[containerViewController setNeedsStatusBarAppearanceUpdate];
|
2014-09-24 05:07:02 +00:00
|
|
|
|
|
|
|
CGRectSetY( destinationViewController.view.frame, 100 );
|
|
|
|
destinationViewController.view.transform = CGAffineTransformMakeScale( 1.2f, 1.2f );
|
|
|
|
destinationViewController.view.alpha = 0;
|
|
|
|
|
2014-09-26 04:32:07 +00:00
|
|
|
[UIView transitionWithView:containerViewController.view duration:0.3f
|
2014-09-23 02:32:31 +00:00
|
|
|
options:UIViewAnimationOptionAllowAnimatedContent animations:^{
|
2014-09-24 05:07:02 +00:00
|
|
|
destinationViewController.view.transform = CGAffineTransformIdentity;
|
2014-09-23 02:32:31 +00:00
|
|
|
CGRectSetY( destinationViewController.view.frame, 0 );
|
2014-09-24 05:07:02 +00:00
|
|
|
destinationViewController.view.alpha = 1;
|
2014-09-27 05:27:05 +00:00
|
|
|
dismissButton.alpha = 1;
|
2014-09-23 02:32:31 +00:00
|
|
|
} completion:^(BOOL finished) {
|
2014-09-26 04:32:07 +00:00
|
|
|
[destinationViewController didMoveToParentViewController:containerViewController];
|
|
|
|
[containerViewController setNeedsStatusBarAppearanceUpdate];
|
2014-09-23 02:32:31 +00:00
|
|
|
}];
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// Unwinding
|
|
|
|
[sourceViewController willMoveToParentViewController:nil];
|
2014-09-24 05:07:02 +00:00
|
|
|
[UIView transitionWithView:containerViewController.view duration:0.2f
|
2014-09-23 02:32:31 +00:00
|
|
|
options:UIViewAnimationOptionAllowAnimatedContent animations:^{
|
2014-09-24 05:07:02 +00:00
|
|
|
CGRectSetY( sourceViewController.view.frame, 100 );
|
|
|
|
sourceViewController.view.transform = CGAffineTransformMakeScale( 0.8f, 0.8f );
|
|
|
|
sourceViewController.view.alpha = 0;
|
|
|
|
[containerViewController removeDismissButtonForViewController:sourceViewController];
|
2014-09-23 02:32:31 +00:00
|
|
|
} completion:^(BOOL finished) {
|
|
|
|
if (finished) {
|
|
|
|
[sourceViewController.view removeFromSuperview];
|
|
|
|
[sourceViewController removeFromParentViewController];
|
2014-09-24 05:07:02 +00:00
|
|
|
[containerViewController setNeedsStatusBarAppearanceUpdate];
|
2014-09-23 02:32:31 +00:00
|
|
|
}
|
|
|
|
}];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|