teratail header banner
teratail header banner
質問するログイン新規登録

回答編集履歴

1

追記

2020/05/18 13:13

投稿

退会済みユーザー
answer CHANGED
@@ -1,3 +1,66 @@
1
1
  ![イメージ説明](499ef58cbcda40730817086eb79f1c73.gif)
2
2
 
3
- [参考にでも](https://github.com/tyobigoro/tTransition)
3
+ [参考にでも](https://github.com/tyobigoro/tTransition)
4
+
5
+ ```swift
6
+ // 遷移時の処理
7
+ func animatePresentTransition(transitionContext: UIViewControllerContextTransitioning) {
8
+ // 遷移元、遷移先、枠、アニメーション用のラベル画像を取得
9
+ let green = transitionContext.viewController(forKey: UITransitionContextViewControllerKey.from) as! GreenVC
10
+ let orange = transitionContext.viewController(forKey: UITransitionContextViewControllerKey.to) as! OrangeVC
11
+ let container: UIView! = transitionContext.containerView
12
+
13
+ let animationLabelImageView = UIImageView(image: green.label.getImage())
14
+
15
+ // アニーメーションラベルの遷移元座標、遷移先座標を取得
16
+ let fromFrame = container.convert(green.label.frame, from: green.view)
17
+ let toFrame = container.convert(orange.label.frame, from: orange.view)
18
+
19
+ // アニメーションラベルに遷移元座標を設定する
20
+ animationLabelImageView.frame = fromFrame
21
+
22
+ // 遷移先を用意する
23
+ orange.view.frame = container.frame
24
+ orange.view.alpha = .zero
25
+
26
+ // 描画する
27
+ container.addSubview(orange.view)
28
+ container.addSubview(animationLabelImageView)
29
+
30
+ // 実在するラベルの設定
31
+ orange.label.isHidden = true
32
+ green.label.isHidden = true
33
+
34
+ // アニメーションしながら遷移元と遷移先を入れ替える
35
+ UIView.animate(withDuration: duration, animations: {
36
+ orange.view.alpha = 1.0
37
+ animationLabelImageView.frame = toFrame
38
+
39
+ }, completion: {_ in
40
+ orange.label.isHidden = false
41
+ green.removeFromParent()
42
+ animationLabelImageView.removeFromSuperview()
43
+ transitionContext.completeTransition(true)
44
+ })
45
+ }
46
+
47
+
48
+ extension UIView {
49
+
50
+ func getImage() -> UIImage{
51
+
52
+ let rect = self.bounds
53
+
54
+ UIGraphicsBeginImageContextWithOptions(rect.size, false, 0.0)
55
+ let context: CGContext = UIGraphicsGetCurrentContext()!
56
+
57
+ self.layer.render(in: context)
58
+
59
+ let capturedImage : UIImage = UIGraphicsGetImageFromCurrentImageContext()!
60
+
61
+ UIGraphicsEndImageContext()
62
+
63
+ return capturedImage
64
+ }
65
+ }
66
+ ```