This repository has been archived by the owner on Aug 13, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 80
/
ModalDialogExample.swift
140 lines (108 loc) · 4.73 KB
/
ModalDialogExample.swift
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
/*
Copyright 2016-present The Material Motion 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 UIKit
import MaterialMotion
class ModalDialogExampleViewController: ExampleViewController {
override func viewDidLoad() {
super.viewDidLoad()
view.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(didTap)))
}
func didTap() {
let vc = ModalDialogViewController()
present(vc, animated: true)
}
func tapToDismiss() {
dismiss(animated: true)
}
override func exampleInformation() -> ExampleInfo {
return .init(title: type(of: self).catalogBreadcrumbs().last!,
instructions: "Tap to present a modal dialog.")
}
}
class ModalDialogViewController: UIViewController {
override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) {
super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
transitionController.transition = ModalDialogTransition()
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .primaryColor
view.layer.cornerRadius = 5
view.layer.shadowColor = UIColor(white: 0, alpha: 0.4).cgColor
view.layer.shadowRadius = 5
view.layer.shadowOpacity = 1
view.layer.shadowOffset = .init(width: 0, height: 2)
}
}
class ModalDialogTransition: SelfDismissingTransition, TransitionWithPresentation {
public func defaultModalPresentationStyle() -> UIModalPresentationStyle? {
return .custom
}
public func presentationController(forPresented presented: UIViewController,
presenting: UIViewController?,
source: UIViewController) -> UIPresentationController? {
return ModalDialogPresentationController(presentedViewController: presented,
presenting: presenting)
}
func willBeginTransition(withContext ctx: TransitionContext, runtime: MotionRuntime) -> [Stateful] {
let size = ctx.fore.view.frame.size
let bounds = ctx.containerView().bounds
let backPosition = CGPoint(x: bounds.midX, y: bounds.maxY + size.height * 3 / 4)
let forePosition = ctx.fore.view.layer.position
let reactiveForeLayer = runtime.get(ctx.fore.view.layer)
let position = reactiveForeLayer.position
let draggable = Draggable(withFirstGestureIn: ctx.gestureRecognizers)
let centerY = ctx.containerView().bounds.height / 2.0
runtime.add(ChangeDirection(withVelocityOf: draggable.nextGestureRecognizer, whenNegative: .forward),
to: ctx.direction)
if let gesture = draggable.nextGestureRecognizer {
runtime.connect(runtime.get(gesture)
.velocityOnReleaseStream()
.y()
.thresholdRange(min: -100, max: 100)
.rewrite([.within: position.y().threshold(centerY).rewrite([.below: .forward,
.above: .backward])]),
to: ctx.direction)
}
let movement = TransitionSpring(back: backPosition,
fore: forePosition,
direction: ctx.direction)
let tossable = Tossable(spring: movement, draggable: draggable)
runtime.add(tossable, to: ctx.fore.view)
return [tossable.spring]
}
func willPresent(fore: UIViewController, dismisser: ViewControllerDismisser) {
let tap = UITapGestureRecognizer()
fore.view.addGestureRecognizer(tap)
dismisser.dismissWhenGestureRecognizerBegins(tap)
let pan = UIPanGestureRecognizer()
fore.view.addGestureRecognizer(pan)
dismisser.dismissWhenGestureRecognizerBegins(pan)
}
}
private final class ModalDialogPresentationController: UIPresentationController {
override var frameOfPresentedViewInContainerView: CGRect {
guard let containerView = containerView else {
assertionFailure("Missing container view during frame query.")
return .zero()
}
let size = CGSize(width: 200, height: 200)
return CGRect(x: containerView.bounds.midX - size.width / 2,
y: containerView.bounds.midY - size.height / 2,
width: size.width,
height: size.height)
}
}