質問編集履歴
4
タイトル:Present → Modal
test
CHANGED
@@ -1 +1 @@
|
|
1
|
-
|
1
|
+
Modalの遷移方法をcrossDissolveにするとポップアップの遷移元画面が真っ黒になる
|
test
CHANGED
File without changes
|
3
誤字の修正
test
CHANGED
@@ -1 +1 @@
|
|
1
|
-
|
1
|
+
Presentの遷移方法をcrossDissolveにするとポップアップの遷移元画面が真っ黒になる
|
test
CHANGED
File without changes
|
2
試したことを追加しました。
test
CHANGED
@@ -1 +1 @@
|
|
1
|
-
Presentの遷移方法をcrossDissolveにするとポップアップの遷移元画面が真っ黒になる
|
1
|
+
asPresentの遷移方法をcrossDissolveにするとポップアップの遷移元画面が真っ黒になる
|
test
CHANGED
@@ -249,3 +249,13 @@
|
|
249
249
|
}
|
250
250
|
|
251
251
|
```
|
252
|
+
|
253
|
+
|
254
|
+
|
255
|
+
試したこと
|
256
|
+
|
257
|
+
・modalPresentationStyleをoverCurrentContextにする
|
258
|
+
|
259
|
+
こちらの方法は、ポップアップが全画面表示になってしまい、
|
260
|
+
|
261
|
+
ポップアップとして機能しなくなってしまいます。
|
1
コードの追加
test
CHANGED
File without changes
|
test
CHANGED
@@ -17,3 +17,235 @@
|
|
17
17
|
【うまく行かない方:crossDissolve】
|
18
18
|
|
19
19
|
![イメージ説明](000ec48306ddb689165b4ac398af626a.png)
|
20
|
+
|
21
|
+
|
22
|
+
|
23
|
+
```Swift
|
24
|
+
|
25
|
+
|
26
|
+
|
27
|
+
import UIKit
|
28
|
+
|
29
|
+
|
30
|
+
|
31
|
+
class ViewController: UIViewController, UIViewControllerTransitioningDelegate {
|
32
|
+
|
33
|
+
|
34
|
+
|
35
|
+
override func viewDidLoad() {
|
36
|
+
|
37
|
+
super.viewDidLoad()
|
38
|
+
|
39
|
+
// Do any additional setup after loading the view.
|
40
|
+
|
41
|
+
}
|
42
|
+
|
43
|
+
|
44
|
+
|
45
|
+
@IBAction func showModal(_ sender: Any) {
|
46
|
+
|
47
|
+
let modalVC = self.storyboard?.instantiateViewController(identifier: "modal")
|
48
|
+
|
49
|
+
|
50
|
+
|
51
|
+
////////////////////////////////////////////////////////////////////////////////////
|
52
|
+
|
53
|
+
modalVC!.modalTransitionStyle = .crossDissolve
|
54
|
+
|
55
|
+
////////////////////////////////////////////////////////////////////////////////////
|
56
|
+
|
57
|
+
|
58
|
+
|
59
|
+
modalVC!.modalPresentationStyle = .custom
|
60
|
+
|
61
|
+
modalVC!.transitioningDelegate = self
|
62
|
+
|
63
|
+
present(modalVC!, animated: true, completion: nil)
|
64
|
+
|
65
|
+
}
|
66
|
+
|
67
|
+
|
68
|
+
|
69
|
+
func presentationController(forPresented presented: UIViewController, presenting: UIViewController?, source: UIViewController) -> UIPresentationController? {
|
70
|
+
|
71
|
+
return PresentationController(presentedViewController: presented, presenting: presenting)
|
72
|
+
|
73
|
+
}
|
74
|
+
|
75
|
+
}
|
76
|
+
|
77
|
+
|
78
|
+
|
79
|
+
//PresentationController.swift
|
80
|
+
|
81
|
+
|
82
|
+
|
83
|
+
//import UIKit
|
84
|
+
|
85
|
+
|
86
|
+
|
87
|
+
class PresentationController: UIPresentationController {
|
88
|
+
|
89
|
+
// 呼び出し元のView Controller の上に重ねるオーバレイView
|
90
|
+
|
91
|
+
var overlayView = UIView()
|
92
|
+
|
93
|
+
|
94
|
+
|
95
|
+
// 表示トランジション開始前に呼ばれる
|
96
|
+
|
97
|
+
override func presentationTransitionWillBegin() {
|
98
|
+
|
99
|
+
guard let containerView = containerView else {
|
100
|
+
|
101
|
+
return
|
102
|
+
|
103
|
+
}
|
104
|
+
|
105
|
+
overlayView.frame = containerView.bounds
|
106
|
+
|
107
|
+
overlayView.gestureRecognizers = [UITapGestureRecognizer(target: self, action: #selector(PresentationController.overlayViewDidTouch(_:)))]
|
108
|
+
|
109
|
+
overlayView.backgroundColor = .black
|
110
|
+
|
111
|
+
overlayView.alpha = 0.0
|
112
|
+
|
113
|
+
containerView.insertSubview(overlayView, at: 0)
|
114
|
+
|
115
|
+
|
116
|
+
|
117
|
+
/// ポップアップ元画面の透明度を濁らせる
|
118
|
+
|
119
|
+
presentedViewController.transitionCoordinator?.animate(alongsideTransition: {[weak self] context in
|
120
|
+
|
121
|
+
self?.overlayView.alpha = 0.5
|
122
|
+
|
123
|
+
}, completion:nil)
|
124
|
+
|
125
|
+
}
|
126
|
+
|
127
|
+
|
128
|
+
|
129
|
+
// 非表示トランジション開始前に呼ばれる
|
130
|
+
|
131
|
+
override func dismissalTransitionWillBegin() {
|
132
|
+
|
133
|
+
/// ポップアップ元画面の透明度をもとに戻す
|
134
|
+
|
135
|
+
presentedViewController.transitionCoordinator?.animate(alongsideTransition: {[weak self] context in
|
136
|
+
|
137
|
+
self?.overlayView.alpha = 0.0
|
138
|
+
|
139
|
+
}, completion:nil)
|
140
|
+
|
141
|
+
}
|
142
|
+
|
143
|
+
|
144
|
+
|
145
|
+
// 非表示トランジション開始後に呼ばれる
|
146
|
+
|
147
|
+
override func dismissalTransitionDidEnd(_ completed: Bool) {
|
148
|
+
|
149
|
+
if completed {
|
150
|
+
|
151
|
+
overlayView.removeFromSuperview()
|
152
|
+
|
153
|
+
}
|
154
|
+
|
155
|
+
}
|
156
|
+
|
157
|
+
|
158
|
+
|
159
|
+
// ポップアップのマージン
|
160
|
+
|
161
|
+
let margin = { () -> CGPoint in
|
162
|
+
|
163
|
+
// 端末の画面サイズ
|
164
|
+
|
165
|
+
let screenWidth = Double(UIScreen.main.bounds.size.width)
|
166
|
+
|
167
|
+
let screenHeight = Double(UIScreen.main.bounds.size.height)
|
168
|
+
|
169
|
+
|
170
|
+
|
171
|
+
let marginX = screenWidth * 0.13
|
172
|
+
|
173
|
+
let marginY = screenHeight * 0.44
|
174
|
+
|
175
|
+
|
176
|
+
|
177
|
+
return CGPoint(x: CGFloat(marginX), y: CGFloat(marginY))
|
178
|
+
|
179
|
+
}
|
180
|
+
|
181
|
+
// 子のコンテナサイズを返す
|
182
|
+
|
183
|
+
override func size(forChildContentContainer container: UIContentContainer, withParentContainerSize parentSize: CGSize) -> CGSize {
|
184
|
+
|
185
|
+
return CGSize(width: parentSize.width - margin().x, height: parentSize.height - margin().y)
|
186
|
+
|
187
|
+
}
|
188
|
+
|
189
|
+
|
190
|
+
|
191
|
+
// 呼び出し先のView Controllerのframeを返す
|
192
|
+
|
193
|
+
override var frameOfPresentedViewInContainerView: CGRect {
|
194
|
+
|
195
|
+
var presentedViewFrame = CGRect()
|
196
|
+
|
197
|
+
let containerBounds = containerView!.bounds
|
198
|
+
|
199
|
+
let childContentSize = size(forChildContentContainer: presentedViewController, withParentContainerSize: containerBounds.size)
|
200
|
+
|
201
|
+
presentedViewFrame.size = childContentSize
|
202
|
+
|
203
|
+
presentedViewFrame.origin.x = margin().x / 2.0
|
204
|
+
|
205
|
+
presentedViewFrame.origin.y = margin().y / 2.0
|
206
|
+
|
207
|
+
|
208
|
+
|
209
|
+
return presentedViewFrame
|
210
|
+
|
211
|
+
}
|
212
|
+
|
213
|
+
|
214
|
+
|
215
|
+
// レイアウト開始前に呼ばれる
|
216
|
+
|
217
|
+
override func containerViewWillLayoutSubviews() {
|
218
|
+
|
219
|
+
overlayView.frame = containerView!.bounds
|
220
|
+
|
221
|
+
presentedView?.frame = frameOfPresentedViewInContainerView
|
222
|
+
|
223
|
+
presentedView?.layer.cornerRadius = 10
|
224
|
+
|
225
|
+
presentedView?.clipsToBounds = true
|
226
|
+
|
227
|
+
}
|
228
|
+
|
229
|
+
|
230
|
+
|
231
|
+
// レイアウト開始後に呼ばれる
|
232
|
+
|
233
|
+
override func containerViewDidLayoutSubviews() {
|
234
|
+
|
235
|
+
}
|
236
|
+
|
237
|
+
|
238
|
+
|
239
|
+
// overlayViewをタップした時に呼ばれる
|
240
|
+
|
241
|
+
@objc func overlayViewDidTouch(_ sender: UITapGestureRecognizer) {
|
242
|
+
|
243
|
+
/// ポップアップ元画面へ戻る
|
244
|
+
|
245
|
+
presentedViewController.dismiss(animated: true, completion: nil)
|
246
|
+
|
247
|
+
}
|
248
|
+
|
249
|
+
}
|
250
|
+
|
251
|
+
```
|