/
MDCDialogPresentationController.m
424 lines (348 loc) · 16 KB
/
MDCDialogPresentationController.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
// 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 "MDCDialogPresentationController.h"
#import "private/MDCDialogShadowedView.h"
#import "MDCDialogPresentationControllerDelegate.h"
#import "MaterialShadowElevations.h"
#import "MaterialKeyboardWatcher.h"
static const CGFloat MDCDialogMinimumWidth = 280;
// TODO: Spec indicates 40 side margins and 280 minimum width.
// That is incompatible with a 320 wide device.
// Side margins set to 20 until we have a resolution
static const UIEdgeInsets MDCDialogEdgeInsets = {24, 20, 24, 20};
@interface MDCDialogPresentationController ()
// View matching the container's bounds that dims the entire screen and catchs taps to dismiss.
@property(nonatomic) UIView *dimmingView;
// Tracking view that adds a shadow under the presented view. This view's frame should always match
// the presented view's.
@property(nonatomic) MDCDialogShadowedView *trackingView;
@end
@implementation MDCDialogPresentationController {
UITapGestureRecognizer *_dismissGestureRecognizer;
BOOL useDialogCornerRadius;
CGFloat previousPresentedViewCornerRadius;
}
#pragma mark - UIPresentationController
// dismissOnBackgroundTap wraps the enable property of our gesture recognizer to
// avoid duplication.
- (void)setDismissOnBackgroundTap:(BOOL)dismissOnBackgroundTap {
_dismissGestureRecognizer.enabled = dismissOnBackgroundTap;
}
- (BOOL)dismissOnBackgroundTap {
return _dismissGestureRecognizer.enabled;
}
// presentedViewCornerRadius wraps the cornerRadius property of our tracking view to avoid
// duplication.
- (void)setDialogCornerRadius:(CGFloat)cornerRadius {
_trackingView.layer.cornerRadius = cornerRadius;
useDialogCornerRadius = YES;
}
- (CGFloat)dialogCornerRadius {
return _trackingView.layer.cornerRadius;
}
- (void)setScrimColor:(UIColor *)scrimColor {
self.dimmingView.backgroundColor = scrimColor;
}
- (UIColor *)scrimColor {
return self.dimmingView.backgroundColor;
}
- (void)setDialogElevation:(MDCShadowElevation)dialogElevation {
_trackingView.elevation = dialogElevation;
}
- (CGFloat)dialogElevation {
return _trackingView.elevation;
}
- (void)setDialogShadowColor:(UIColor *)dialogShadowColor {
self.trackingView.shadowColor = dialogShadowColor;
}
- (UIColor *)dialogShadowColor {
return self.trackingView.shadowColor;
}
- (instancetype)initWithPresentedViewController:(UIViewController *)presentedViewController
presentingViewController:(UIViewController *)presentingViewController {
self = [super initWithPresentedViewController:presentedViewController
presentingViewController:presentingViewController];
if (self) {
useDialogCornerRadius = NO;
_dimmingView = [[UIView alloc] initWithFrame:CGRectZero];
_dimmingView.backgroundColor = [UIColor colorWithWhite:0 alpha:(CGFloat)0.32];
_dimmingView.alpha = 0;
_dismissGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self
action:@selector(dismiss:)];
[_dimmingView addGestureRecognizer:_dismissGestureRecognizer];
_trackingView = [[MDCDialogShadowedView alloc] init];
_trackingView.shadowColor = UIColor.blackColor;
[self registerKeyboardNotifications];
}
return self;
}
- (void)dealloc {
[self unregisterKeyboardNotifications];
}
- (void)traitCollectionDidChange:(UITraitCollection *)previousTraitCollection {
[super traitCollectionDidChange:previousTraitCollection];
if (self.traitCollectionDidChangeBlock) {
self.traitCollectionDidChangeBlock(self, previousTraitCollection);
}
}
- (CGRect)frameOfPresentedViewInContainerView {
CGRect containerBounds = CGRectStandardize(self.containerView.bounds);
// For pre iOS 11 devices, we are assuming a safeAreaInset of UIEdgeInsetsZero
UIEdgeInsets containerSafeAreaInsets = UIEdgeInsetsZero;
if (@available(iOS 11.0, *)) {
containerSafeAreaInsets = self.containerView.safeAreaInsets;
}
// Take the larger of the Safe Area insets and the Material specified insets.
containerSafeAreaInsets.top = MAX(containerSafeAreaInsets.top, MDCDialogEdgeInsets.top);
containerSafeAreaInsets.left = MAX(containerSafeAreaInsets.left, MDCDialogEdgeInsets.left);
containerSafeAreaInsets.right = MAX(containerSafeAreaInsets.right, MDCDialogEdgeInsets.right);
containerSafeAreaInsets.bottom = MAX(containerSafeAreaInsets.bottom, MDCDialogEdgeInsets.bottom);
// Take into account a visible keyboard
CGFloat keyboardHeight = [MDCKeyboardWatcher sharedKeyboardWatcher].visibleKeyboardHeight;
containerSafeAreaInsets.bottom = MAX(containerSafeAreaInsets.bottom, keyboardHeight);
// Area that the presented dialog can use.
CGRect standardPresentableBounds =
UIEdgeInsetsInsetRect(containerBounds, containerSafeAreaInsets);
CGRect presentedViewFrame = CGRectZero;
presentedViewFrame.size = [self sizeForChildContentContainer:self.presentedViewController
withParentContainerSize:standardPresentableBounds.size];
presentedViewFrame.origin.x =
containerSafeAreaInsets.left +
(standardPresentableBounds.size.width - presentedViewFrame.size.width) * (CGFloat)0.5;
presentedViewFrame.origin.y =
containerSafeAreaInsets.top +
(standardPresentableBounds.size.height - presentedViewFrame.size.height) * (CGFloat)0.5;
presentedViewFrame.origin.x = (CGFloat)floor(presentedViewFrame.origin.x);
presentedViewFrame.origin.y = (CGFloat)floor(presentedViewFrame.origin.y);
return presentedViewFrame;
}
- (void)presentationTransitionWillBegin {
// TODO: Follow the Material spec description of Autonomous surface creation for both
// presentation and dismissal of the dialog.
// https://material.io/guidelines/motion/choreography.html#choreography-creation
// Ensure the same corner radius is used by both the tracking view and the view being presented
if (useDialogCornerRadius) {
// If dialogCornerRadius is set, use its value for the shadow layer as well as the presented
// view layer, overriding any direct assignments to the presented view's cornerRadius.
_trackingView.layer.cornerRadius = self.dialogCornerRadius;
// Note: For MDCAlertController, this assumes that the "view" property points to the same
// instance as the "alertView" property. Therefore, we are safe to not set its cornerRadius
// property (its ".cornerRadius", rather then its "presentedView.layer.cornerRadius")
previousPresentedViewCornerRadius = self.presentedView.layer.cornerRadius;
self.presentedView.layer.cornerRadius = self.dialogCornerRadius;
} else {
// If dialogCornerRadius is not set, use the presented view's cornerRadius for the shadow layer.
_trackingView.layer.cornerRadius = self.presentedView.layer.cornerRadius;
}
// Set the dimming view to the container's bounds and fully transparent.
self.dimmingView.frame = self.containerView.bounds;
self.dimmingView.alpha = 0;
[self.containerView addSubview:self.dimmingView];
// Set the shadowing view to the same frame as the presented view.
CGRect presentedFrame = [self frameOfPresentedViewInContainerView];
self.trackingView.frame = presentedFrame;
self.trackingView.alpha = 0;
[self.containerView addSubview:self.trackingView];
// Fade-in chrome views to be fully visible.
id<UIViewControllerTransitionCoordinator> transitionCoordinator =
[self.presentedViewController transitionCoordinator];
if (transitionCoordinator) {
[transitionCoordinator
animateAlongsideTransition:^(
__unused id<UIViewControllerTransitionCoordinatorContext> context) {
self.dimmingView.alpha = 1;
self.trackingView.alpha = 1;
}
completion:NULL];
} else {
self.dimmingView.alpha = 1;
self.trackingView.alpha = 1;
}
}
- (void)presentationTransitionDidEnd:(BOOL)completed {
if (completed) {
// Stop the presenting view from being tapped for voiceover while this view is up.
// Setting @c accessibilityViewIsModal on the presented view (or its parent) should be enough,
// but it's not.
// b/19519321
self.presentingViewController.view.accessibilityElementsHidden = YES;
self.presentedView.accessibilityViewIsModal = YES;
UIAccessibilityPostNotification(UIAccessibilityScreenChangedNotification, [self presentedView]);
} else {
// Transition was cancelled.
[self.dimmingView removeFromSuperview];
[self.trackingView removeFromSuperview];
}
}
- (void)dismissalTransitionWillBegin {
// Fade-out dimmingView and trackingView to be fully transparent.
id<UIViewControllerTransitionCoordinator> transitionCoordinator =
[self.presentedViewController transitionCoordinator];
if (transitionCoordinator != nil) {
[transitionCoordinator
animateAlongsideTransition:^(
__unused id<UIViewControllerTransitionCoordinatorContext> context) {
self.dimmingView.alpha = 0;
self.trackingView.alpha = 0;
}
completion:NULL];
} else {
self.dimmingView.alpha = 0;
self.trackingView.alpha = 0;
}
}
- (CGAffineTransform)dialogTransform {
return self.trackingView.transform;
}
- (void)setDialogTransform:(CGAffineTransform)shadowTransform {
self.trackingView.transform = shadowTransform;
}
- (void)dismissalTransitionDidEnd:(BOOL)completed {
if (completed) {
[self.dimmingView removeFromSuperview];
[self.trackingView removeFromSuperview];
// Re-enable accessibilityElements on the presenting view controller.
self.presentingViewController.view.accessibilityElementsHidden = NO;
if (useDialogCornerRadius) {
// Restore cornerRadius in case it had changed during presentation
self.presentedView.layer.cornerRadius = previousPresentedViewCornerRadius;
}
}
[super dismissalTransitionDidEnd:completed];
}
/**
Indicate that the presenting view controller's view should not be removed when the presentation
animations finish.
MDCDialogPresentationController does not cover the presenting view controller's content entirely
so we must return NO.
*/
- (BOOL)shouldRemovePresentersView {
return NO;
}
#pragma mark - UIContentContainer
/**
Determines the size of the presented container's view based on available space and the preferred
content size of the container.
*/
- (CGSize)sizeForChildContentContainer:(id<UIContentContainer>)container
withParentContainerSize:(CGSize)parentSize {
if (CGSizeEqualToSize(parentSize, CGSizeZero)) {
return CGSizeZero;
}
CGSize targetSize = parentSize;
const CGSize preferredContentSize = container.preferredContentSize;
if (!CGSizeEqualToSize(preferredContentSize, CGSizeZero)) {
targetSize = preferredContentSize;
// If the targetSize.width is greater than 0.0 it must be at least MDCDialogMinimumWidth.
if (0 < targetSize.width && targetSize.width < MDCDialogMinimumWidth) {
targetSize.width = MDCDialogMinimumWidth;
}
// targetSize cannot exceed parentSize.
targetSize.width = MIN(targetSize.width, parentSize.width);
targetSize.height = MIN(targetSize.height, parentSize.height);
}
targetSize.width = (CGFloat)ceil(targetSize.width);
targetSize.height = (CGFloat)ceil(targetSize.height);
return targetSize;
}
- (void)viewWillTransitionToSize:(CGSize)size
withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator {
[super viewWillTransitionToSize:size withTransitionCoordinator:coordinator];
[coordinator
animateAlongsideTransition:^(
__unused id<UIViewControllerTransitionCoordinatorContext> context) {
self.dimmingView.frame = self.containerView.bounds;
CGRect presentedViewFrame = [self frameOfPresentedViewInContainerView];
self.presentedView.frame = presentedViewFrame;
self.trackingView.frame = presentedViewFrame;
}
completion:NULL];
}
/**
If the container's preferred content size has changed and we are able to accommidate the new size,
update the frame of the presented view and the shadowing view.
*/
- (void)preferredContentSizeDidChangeForChildContentContainer:(id<UIContentContainer>)container {
[super preferredContentSizeDidChangeForChildContentContainer:container];
CGSize existingSize = self.presentedView.bounds.size;
CGSize newSize = [self sizeForChildContentContainer:container
withParentContainerSize:self.containerView.bounds.size];
if (!CGSizeEqualToSize(existingSize, newSize)) {
CGRect presentedViewFrame = [self frameOfPresentedViewInContainerView];
self.presentedView.frame = presentedViewFrame;
self.trackingView.frame = presentedViewFrame;
}
}
#pragma mark - Internal
- (void)dismiss:(UIGestureRecognizer *)gesture {
if (gesture.state == UIGestureRecognizerStateRecognized) {
[self.presentingViewController
dismissViewControllerAnimated:YES
completion:^{
if ([self.dialogPresentationControllerDelegate
respondsToSelector:@selector
(dialogPresentationControllerDidDismiss:)]) {
[self.dialogPresentationControllerDelegate
dialogPresentationControllerDidDismiss:self];
}
}];
}
}
#pragma mark - Keyboard handling
- (void)registerKeyboardNotifications {
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWatcherHandler:)
name:MDCKeyboardWatcherKeyboardWillShowNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWatcherHandler:)
name:MDCKeyboardWatcherKeyboardWillHideNotification
object:nil];
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:@selector(keyboardWatcherHandler:)
name:MDCKeyboardWatcherKeyboardWillChangeFrameNotification
object:nil];
}
- (void)unregisterKeyboardNotifications {
[[NSNotificationCenter defaultCenter]
removeObserver:self
name:MDCKeyboardWatcherKeyboardWillShowNotification
object:nil];
[[NSNotificationCenter defaultCenter]
removeObserver:self
name:MDCKeyboardWatcherKeyboardWillHideNotification
object:nil];
[[NSNotificationCenter defaultCenter]
removeObserver:self
name:MDCKeyboardWatcherKeyboardWillChangeFrameNotification
object:nil];
}
#pragma mark - KeyboardWatcher Notifications
- (void)keyboardWatcherHandler:(NSNotification *)aNotification {
NSTimeInterval animationDuration =
[MDCKeyboardWatcher animationDurationFromKeyboardNotification:aNotification];
UIViewAnimationOptions animationCurveOption =
[MDCKeyboardWatcher animationCurveOptionFromKeyboardNotification:aNotification];
[UIView animateWithDuration:animationDuration
delay:0
options:animationCurveOption | UIViewAnimationOptionTransitionNone
animations:^{
CGRect presentedViewFrame = [self frameOfPresentedViewInContainerView];
self.presentedView.frame = presentedViewFrame;
self.trackingView.frame = presentedViewFrame;
}
completion:NULL];
}
@end