質問編集履歴
4
タイトル:Present → Modal
title
CHANGED
@@ -1,1 +1,1 @@
|
|
1
|
-
|
1
|
+
Modalの遷移方法をcrossDissolveにするとポップアップの遷移元画面が真っ黒になる
|
body
CHANGED
File without changes
|
3
誤字の修正
title
CHANGED
@@ -1,1 +1,1 @@
|
|
1
|
-
|
1
|
+
Presentの遷移方法をcrossDissolveにするとポップアップの遷移元画面が真っ黒になる
|
body
CHANGED
File without changes
|
2
試したことを追加しました。
title
CHANGED
@@ -1,1 +1,1 @@
|
|
1
|
-
Presentの遷移方法をcrossDissolveにするとポップアップの遷移元画面が真っ黒になる
|
1
|
+
asPresentの遷移方法をcrossDissolveにするとポップアップの遷移元画面が真っ黒になる
|
body
CHANGED
@@ -123,4 +123,9 @@
|
|
123
123
|
presentedViewController.dismiss(animated: true, completion: nil)
|
124
124
|
}
|
125
125
|
}
|
126
|
-
```
|
126
|
+
```
|
127
|
+
|
128
|
+
試したこと
|
129
|
+
・modalPresentationStyleをoverCurrentContextにする
|
130
|
+
こちらの方法は、ポップアップが全画面表示になってしまい、
|
131
|
+
ポップアップとして機能しなくなってしまいます。
|
1
コードの追加
title
CHANGED
File without changes
|
body
CHANGED
@@ -7,4 +7,120 @@
|
|
7
7
|

|
8
8
|
|
9
9
|
【うまく行かない方:crossDissolve】
|
10
|
-

|
10
|
+

|
11
|
+
|
12
|
+
```Swift
|
13
|
+
|
14
|
+
import UIKit
|
15
|
+
|
16
|
+
class ViewController: UIViewController, UIViewControllerTransitioningDelegate {
|
17
|
+
|
18
|
+
override func viewDidLoad() {
|
19
|
+
super.viewDidLoad()
|
20
|
+
// Do any additional setup after loading the view.
|
21
|
+
}
|
22
|
+
|
23
|
+
@IBAction func showModal(_ sender: Any) {
|
24
|
+
let modalVC = self.storyboard?.instantiateViewController(identifier: "modal")
|
25
|
+
|
26
|
+
////////////////////////////////////////////////////////////////////////////////////
|
27
|
+
modalVC!.modalTransitionStyle = .crossDissolve
|
28
|
+
////////////////////////////////////////////////////////////////////////////////////
|
29
|
+
|
30
|
+
modalVC!.modalPresentationStyle = .custom
|
31
|
+
modalVC!.transitioningDelegate = self
|
32
|
+
present(modalVC!, animated: true, completion: nil)
|
33
|
+
}
|
34
|
+
|
35
|
+
func presentationController(forPresented presented: UIViewController, presenting: UIViewController?, source: UIViewController) -> UIPresentationController? {
|
36
|
+
return PresentationController(presentedViewController: presented, presenting: presenting)
|
37
|
+
}
|
38
|
+
}
|
39
|
+
|
40
|
+
//PresentationController.swift
|
41
|
+
|
42
|
+
//import UIKit
|
43
|
+
|
44
|
+
class PresentationController: UIPresentationController {
|
45
|
+
// 呼び出し元のView Controller の上に重ねるオーバレイView
|
46
|
+
var overlayView = UIView()
|
47
|
+
|
48
|
+
// 表示トランジション開始前に呼ばれる
|
49
|
+
override func presentationTransitionWillBegin() {
|
50
|
+
guard let containerView = containerView else {
|
51
|
+
return
|
52
|
+
}
|
53
|
+
overlayView.frame = containerView.bounds
|
54
|
+
overlayView.gestureRecognizers = [UITapGestureRecognizer(target: self, action: #selector(PresentationController.overlayViewDidTouch(_:)))]
|
55
|
+
overlayView.backgroundColor = .black
|
56
|
+
overlayView.alpha = 0.0
|
57
|
+
containerView.insertSubview(overlayView, at: 0)
|
58
|
+
|
59
|
+
/// ポップアップ元画面の透明度を濁らせる
|
60
|
+
presentedViewController.transitionCoordinator?.animate(alongsideTransition: {[weak self] context in
|
61
|
+
self?.overlayView.alpha = 0.5
|
62
|
+
}, completion:nil)
|
63
|
+
}
|
64
|
+
|
65
|
+
// 非表示トランジション開始前に呼ばれる
|
66
|
+
override func dismissalTransitionWillBegin() {
|
67
|
+
/// ポップアップ元画面の透明度をもとに戻す
|
68
|
+
presentedViewController.transitionCoordinator?.animate(alongsideTransition: {[weak self] context in
|
69
|
+
self?.overlayView.alpha = 0.0
|
70
|
+
}, completion:nil)
|
71
|
+
}
|
72
|
+
|
73
|
+
// 非表示トランジション開始後に呼ばれる
|
74
|
+
override func dismissalTransitionDidEnd(_ completed: Bool) {
|
75
|
+
if completed {
|
76
|
+
overlayView.removeFromSuperview()
|
77
|
+
}
|
78
|
+
}
|
79
|
+
|
80
|
+
// ポップアップのマージン
|
81
|
+
let margin = { () -> CGPoint in
|
82
|
+
// 端末の画面サイズ
|
83
|
+
let screenWidth = Double(UIScreen.main.bounds.size.width)
|
84
|
+
let screenHeight = Double(UIScreen.main.bounds.size.height)
|
85
|
+
|
86
|
+
let marginX = screenWidth * 0.13
|
87
|
+
let marginY = screenHeight * 0.44
|
88
|
+
|
89
|
+
return CGPoint(x: CGFloat(marginX), y: CGFloat(marginY))
|
90
|
+
}
|
91
|
+
// 子のコンテナサイズを返す
|
92
|
+
override func size(forChildContentContainer container: UIContentContainer, withParentContainerSize parentSize: CGSize) -> CGSize {
|
93
|
+
return CGSize(width: parentSize.width - margin().x, height: parentSize.height - margin().y)
|
94
|
+
}
|
95
|
+
|
96
|
+
// 呼び出し先のView Controllerのframeを返す
|
97
|
+
override var frameOfPresentedViewInContainerView: CGRect {
|
98
|
+
var presentedViewFrame = CGRect()
|
99
|
+
let containerBounds = containerView!.bounds
|
100
|
+
let childContentSize = size(forChildContentContainer: presentedViewController, withParentContainerSize: containerBounds.size)
|
101
|
+
presentedViewFrame.size = childContentSize
|
102
|
+
presentedViewFrame.origin.x = margin().x / 2.0
|
103
|
+
presentedViewFrame.origin.y = margin().y / 2.0
|
104
|
+
|
105
|
+
return presentedViewFrame
|
106
|
+
}
|
107
|
+
|
108
|
+
// レイアウト開始前に呼ばれる
|
109
|
+
override func containerViewWillLayoutSubviews() {
|
110
|
+
overlayView.frame = containerView!.bounds
|
111
|
+
presentedView?.frame = frameOfPresentedViewInContainerView
|
112
|
+
presentedView?.layer.cornerRadius = 10
|
113
|
+
presentedView?.clipsToBounds = true
|
114
|
+
}
|
115
|
+
|
116
|
+
// レイアウト開始後に呼ばれる
|
117
|
+
override func containerViewDidLayoutSubviews() {
|
118
|
+
}
|
119
|
+
|
120
|
+
// overlayViewをタップした時に呼ばれる
|
121
|
+
@objc func overlayViewDidTouch(_ sender: UITapGestureRecognizer) {
|
122
|
+
/// ポップアップ元画面へ戻る
|
123
|
+
presentedViewController.dismiss(animated: true, completion: nil)
|
124
|
+
}
|
125
|
+
}
|
126
|
+
```
|