/
MDCDialogTransitionController.m
112 lines (87 loc) · 4.29 KB
/
MDCDialogTransitionController.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/*
Copyright 2016-present the Material Components for iOS authors. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#import "MDCDialogTransitionController.h"
#import "MDCDialogPresentationController.h"
@implementation MDCDialogTransitionController
static const NSTimeInterval MDCDialogTransitionDuration = 0.27;
#pragma mark - UIViewControllerAnimatedTransitioning
- (NSTimeInterval)transitionDuration:(id<UIViewControllerContextTransitioning>)transitionContext {
return MDCDialogTransitionDuration;
}
- (void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext {
// If a view in the transitionContext is nil, it likely hasn't been loaded by its ViewController
// yet. Ask for it directly to initiate a loadView on the ViewController.
UIViewController *fromViewController =
[transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
UIView *fromView = [transitionContext viewForKey:UITransitionContextFromViewKey];
if (fromView == nil) {
fromView = fromViewController.view;
}
UIViewController *toViewController =
[transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
UIView *toView = [transitionContext viewForKey:UITransitionContextToViewKey];
if (toView == nil) {
toView = toViewController.view;
}
UIViewController *toPresentingViewController = toViewController.presentingViewController;
BOOL presenting = (toPresentingViewController == fromViewController) ? YES : NO;
UIViewController *animatingViewController = presenting ? toViewController : fromViewController;
UIView *animatingView = presenting ? toView : fromView;
UIView *containerView = transitionContext.containerView;
if (presenting) {
[containerView addSubview:toView];
}
CGFloat startingAlpha = presenting ? 0.0f : 1.0f;
CGFloat endingAlpha = presenting ? 1.0f : 0.0f;
animatingView.frame = [transitionContext finalFrameForViewController:animatingViewController];
animatingView.alpha = startingAlpha;
NSTimeInterval transitionDuration = [self transitionDuration:transitionContext];
UIViewAnimationOptions options =
UIViewAnimationOptionAllowUserInteraction | UIViewAnimationOptionBeginFromCurrentState;
[UIView animateWithDuration:transitionDuration
delay:0.0
options:options
animations:^{
animatingView.alpha = endingAlpha;
}
completion:^(BOOL finished) {
// If we're dismissing, remove the presented view from the hierarchy
if (!presenting) {
[fromView removeFromSuperview];
}
// From ADC : UIViewControllerContextTransitioning
// When you do create transition animations, always call the
// completeTransition: from an appropriate completion block to let UIKit know
// when all of your animations have finished.
[transitionContext completeTransition:YES];
}];
}
#pragma mark - UIViewControllerTransitioningDelegate
- (UIPresentationController *)
presentationControllerForPresentedViewController:(UIViewController *)presented
presentingViewController:(UIViewController *)presenting
sourceViewController:(UIViewController *)source {
return [[MDCDialogPresentationController alloc] initWithPresentedViewController:presented
presentingViewController:presenting];
}
- (id<UIViewControllerAnimatedTransitioning>)
animationControllerForPresentedController:(UIViewController *)presented
presentingController:(UIViewController *)presenting
sourceController:(UIViewController *)source {
return self;
}
- (id<UIViewControllerAnimatedTransitioning>)animationControllerForDismissedController:
(UIViewController *)dismissed {
return self;
}
@end