回答編集履歴

1

追記

2020/05/18 13:13

投稿

退会済みユーザー
test CHANGED
@@ -3,3 +3,129 @@
3
3
 
4
4
 
5
5
  [参考にでも](https://github.com/tyobigoro/tTransition)
6
+
7
+
8
+
9
+ ```swift
10
+
11
+ // 遷移時の処理
12
+
13
+ func animatePresentTransition(transitionContext: UIViewControllerContextTransitioning) {
14
+
15
+ // 遷移元、遷移先、枠、アニメーション用のラベル画像を取得
16
+
17
+ let green = transitionContext.viewController(forKey: UITransitionContextViewControllerKey.from) as! GreenVC
18
+
19
+ let orange = transitionContext.viewController(forKey: UITransitionContextViewControllerKey.to) as! OrangeVC
20
+
21
+ let container: UIView! = transitionContext.containerView
22
+
23
+
24
+
25
+ let animationLabelImageView = UIImageView(image: green.label.getImage())
26
+
27
+
28
+
29
+ // アニーメーションラベルの遷移元座標、遷移先座標を取得
30
+
31
+ let fromFrame = container.convert(green.label.frame, from: green.view)
32
+
33
+ let toFrame = container.convert(orange.label.frame, from: orange.view)
34
+
35
+
36
+
37
+ // アニメーションラベルに遷移元座標を設定する
38
+
39
+ animationLabelImageView.frame = fromFrame
40
+
41
+
42
+
43
+ // 遷移先を用意する
44
+
45
+ orange.view.frame = container.frame
46
+
47
+ orange.view.alpha = .zero
48
+
49
+
50
+
51
+ // 描画する
52
+
53
+ container.addSubview(orange.view)
54
+
55
+ container.addSubview(animationLabelImageView)
56
+
57
+
58
+
59
+ // 実在するラベルの設定
60
+
61
+ orange.label.isHidden = true
62
+
63
+ green.label.isHidden = true
64
+
65
+
66
+
67
+ // アニメーションしながら遷移元と遷移先を入れ替える
68
+
69
+ UIView.animate(withDuration: duration, animations: {
70
+
71
+ orange.view.alpha = 1.0
72
+
73
+ animationLabelImageView.frame = toFrame
74
+
75
+
76
+
77
+ }, completion: {_ in
78
+
79
+ orange.label.isHidden = false
80
+
81
+ green.removeFromParent()
82
+
83
+ animationLabelImageView.removeFromSuperview()
84
+
85
+ transitionContext.completeTransition(true)
86
+
87
+ })
88
+
89
+ }
90
+
91
+
92
+
93
+
94
+
95
+ extension UIView {
96
+
97
+
98
+
99
+ func getImage() -> UIImage{
100
+
101
+
102
+
103
+ let rect = self.bounds
104
+
105
+
106
+
107
+ UIGraphicsBeginImageContextWithOptions(rect.size, false, 0.0)
108
+
109
+ let context: CGContext = UIGraphicsGetCurrentContext()!
110
+
111
+
112
+
113
+ self.layer.render(in: context)
114
+
115
+
116
+
117
+ let capturedImage : UIImage = UIGraphicsGetImageFromCurrentImageContext()!
118
+
119
+
120
+
121
+ UIGraphicsEndImageContext()
122
+
123
+
124
+
125
+ return capturedImage
126
+
127
+ }
128
+
129
+ }
130
+
131
+ ```