@@ -9,12 +9,12 @@ import SnapKit
9
9
import UIKit
10
10
11
11
extension CLAnimationTransitioning {
12
- enum CLAnimationType {
12
+ enum AnimationType {
13
13
case present
14
14
case dismiss
15
15
}
16
16
17
- enum CLAnimationOrientation {
17
+ enum AnimationOrientation {
18
18
case left
19
19
case right
20
20
case fullRight
@@ -30,24 +30,27 @@ class CLAnimationTransitioning: NSObject {
30
30
}
31
31
} ( )
32
32
33
- private weak var player : CLPlayerView ?
33
+ private weak var playerView : CLPlayerView ?
34
34
35
- private weak var parentView : UIStackView ?
35
+ private weak var parentStackView : UIStackView ?
36
36
37
- private var centerInParent : CGPoint = . zero
37
+ private var initialCenter : CGPoint = . zero
38
38
39
- private var originSize : CGSize = . zero
39
+ private var finalCenter : CGPoint = . zero
40
40
41
- private var orientation : CLAnimationOrientation = . left
41
+ private var initialBounds : CGRect = . zero
42
42
43
- var animationType : CLAnimationType = . present
43
+ private var animationOrientation : AnimationOrientation = . left
44
44
45
- init ( playView: CLPlayerView , orientation: CLAnimationOrientation ) {
46
- player = playView
47
- self . orientation = orientation
48
- parentView = playView. superview as? UIStackView
49
- centerInParent = playView. center
50
- originSize = playView. frame. size
45
+ var animationType : AnimationType = . present
46
+
47
+ init ( playerView: CLPlayerView , animationOrientation: AnimationOrientation ) {
48
+ self . playerView = playerView
49
+ self . animationOrientation = animationOrientation
50
+ parentStackView = playerView. superview as? UIStackView
51
+ initialBounds = playerView. bounds
52
+ initialCenter = playerView. center
53
+ finalCenter = playerView. convert ( initialCenter, to: nil )
51
54
}
52
55
}
53
56
@@ -57,97 +60,59 @@ extension CLAnimationTransitioning: UIViewControllerAnimatedTransitioning {
57
60
}
58
61
59
62
func animateTransition( using transitionContext: UIViewControllerContextTransitioning ) {
60
- guard let playView = player else { return }
63
+ guard let playerView = playerView else { return }
64
+
61
65
if animationType == . present {
62
66
guard let toView = transitionContext. view ( forKey: . to) else { return }
63
- guard let fromController = transitionContext. viewController ( forKey: . from) else { return }
64
-
65
- let fromCenter = transitionContext. containerView. convert ( playView. center, from: playView. superview)
66
- let fromSize = transitionContext. containerView. convert ( playView. frame, from: nil ) . size
67
+ guard let toController = transitionContext. viewController ( forKey: . to) as? CLFullScreenController else { return }
67
68
69
+ let startCenter = transitionContext. containerView. convert ( initialCenter, from: playerView)
68
70
transitionContext. containerView. addSubview ( toView)
69
- toView. addSubview ( playView)
70
-
71
- if orientation == . left,
72
- !( fromController. shouldAutorotate && fromController. supportedInterfaceOrientations. contains ( . landscapeLeft) )
73
- {
74
- toView. transform = . init( rotationAngle: - Double. pi * 0.5 )
75
- } else if orientation == . right,
76
- !( fromController. shouldAutorotate && fromController. supportedInterfaceOrientations. contains ( . landscapeRight) )
77
- {
78
- toView. transform = . init( rotationAngle: Double . pi * 0.5 )
79
- } else if orientation == . fullRight {
80
- toView. transform = . init( rotationAngle: - Double. pi * 0.5 )
81
- }
82
-
83
- toView. snp. remakeConstraints { make in
84
- make. center. equalTo ( fromCenter)
85
- make. size. equalTo ( fromSize)
86
- }
87
- playView. snp. remakeConstraints { make in
88
- make. edges. equalToSuperview ( )
89
- }
90
- transitionContext. containerView. setNeedsLayout ( )
91
- transitionContext. containerView. layoutIfNeeded ( )
71
+ toController. mainStackView. addArrangedSubview ( playerView)
72
+ toView. bounds = initialBounds
73
+ toView. center = startCenter
74
+ toView. transform = . init( rotationAngle: toController. isKind ( of: CLFullScreenLeftController . self) ? Double . pi * 0.5 : Double . pi * - 0.5 )
92
75
93
- toView. snp. updateConstraints { make in
94
- make. center. equalTo ( transitionContext. containerView. center)
95
- make. size. equalTo ( transitionContext. containerView. bounds. size)
96
- }
97
76
if #available( iOS 11 . 0 , * ) {
98
- playView . contentView. animationLayout ( safeAreaInsets: keyWindow? . safeAreaInsets ?? . zero, to: . fullScreen)
77
+ playerView . contentView. animationLayout ( safeAreaInsets: keyWindow? . safeAreaInsets ?? . zero, to: . fullScreen)
99
78
} else {
100
- playView . contentView. animationLayout ( safeAreaInsets: . zero, to: . fullScreen)
79
+ playerView . contentView. animationLayout ( safeAreaInsets: . zero, to: . fullScreen)
101
80
}
102
81
UIView . animate ( withDuration: transitionDuration ( using: transitionContext) , delay: 0 , options: . layoutSubviews, animations: {
103
82
toView. transform = . identity
104
- transitionContext. containerView. setNeedsLayout ( )
105
- transitionContext. containerView. layoutIfNeeded ( )
106
- playView . contentView. setNeedsLayout ( )
107
- playView . contentView. layoutIfNeeded ( )
83
+ toView . bounds = transitionContext. containerView. bounds
84
+ toView . center = transitionContext. containerView. center
85
+ playerView . contentView. setNeedsLayout ( )
86
+ playerView . contentView. layoutIfNeeded ( )
108
87
} ) { _ in
88
+ toView. transform = . identity
89
+ toView. bounds = transitionContext. containerView. bounds
90
+ toView. center = transitionContext. containerView. center
109
91
transitionContext. completeTransition ( true )
110
92
UIViewController . attemptRotationToDeviceOrientation ( )
111
93
}
112
94
} else {
113
- guard let parentView = parentView else { return }
95
+ guard let parentStackView = parentStackView else { return }
114
96
guard let fromView = transitionContext. view ( forKey: . from) else { return }
115
97
guard let toView = transitionContext. view ( forKey: . to) else { return }
116
98
117
- toView. frame = transitionContext. containerView. bounds
118
-
119
- let fromCenter = CGPoint ( x: toView. frame. width * 0.5 , y: toView. frame. height * 0.5 )
120
- let fromSize = transitionContext. containerView. convert ( playView. frame, from: nil ) . size
121
-
122
99
transitionContext. containerView. addSubview ( toView)
123
100
transitionContext. containerView. addSubview ( fromView)
101
+ toView. frame = transitionContext. containerView. bounds
124
102
125
- fromView. snp. remakeConstraints { make in
126
- make. center. equalTo ( fromCenter)
127
- make. size. equalTo ( fromSize)
128
- }
129
-
130
- transitionContext. containerView. setNeedsLayout ( )
131
- transitionContext. containerView. layoutIfNeeded ( )
132
-
133
- let center = transitionContext. containerView. convert ( centerInParent, from: parentView)
134
- fromView. snp. updateConstraints { make in
135
- make. center. equalTo ( center)
136
- make. size. equalTo ( originSize)
137
- }
138
-
139
- playView. contentView. animationLayout ( safeAreaInsets: . zero, to: . small)
140
-
103
+ playerView. contentView. animationLayout ( safeAreaInsets: . zero, to: . small)
141
104
UIView . animate ( withDuration: transitionDuration ( using: transitionContext) , delay: 0.0 , options: . layoutSubviews, animations: {
142
105
fromView. transform = . identity
143
- transitionContext . containerView . setNeedsLayout ( )
144
- transitionContext . containerView . layoutIfNeeded ( )
145
- playView . contentView. setNeedsLayout ( )
146
- playView . contentView. layoutIfNeeded ( )
106
+ fromView . center = self . finalCenter
107
+ fromView . bounds = self . initialBounds
108
+ playerView . contentView. setNeedsLayout ( )
109
+ playerView . contentView. layoutIfNeeded ( )
147
110
} ) { _ in
148
111
fromView. transform = . identity
112
+ fromView. center = self . finalCenter
113
+ fromView. bounds = self . initialBounds
114
+ parentStackView. addArrangedSubview ( playerView)
149
115
fromView. removeFromSuperview ( )
150
- parentView. addArrangedSubview ( playView)
151
116
transitionContext. completeTransition ( true )
152
117
UIViewController . attemptRotationToDeviceOrientation ( )
153
118
}
0 commit comments