CNUpSlideAnimationTransition.swift
13.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
//
// CNUpSlideAnimationTransition.swift
// metaCode
//
// Created by zx on 2023/10/16.
//
import UIKit
fileprivate let transitonDuration: TimeInterval = 1.0
enum AnimationType {
case present
case dismiss
}
class CNUpSlideAnimationTransition: NSObject {
let animationType: AnimationType!
var cellType:CNLiveHomePageCellType = .course
init(animationType: AnimationType,cellType:CNLiveHomePageCellType) {
self.cellType = cellType
self.animationType = animationType
super.init()
}
}
extension CNUpSlideAnimationTransition: UIViewControllerAnimatedTransitioning {
func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval {
return transitonDuration
}
func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
if animationType == .present {
animationForPresent(using: transitionContext)
} else {
animationForDismiss(using: transitionContext)
}
}
func animationForPresent(using transitionContext: UIViewControllerContextTransitioning) {
let containerView = transitionContext.containerView
if self.cellType == .playlet{
//1.Get fromVC and toVC
guard let fromVCtemp = transitionContext.viewController(forKey: .from) as? CNLiveBaseTabBarViewController else { return }
let fromNavi = fromVCtemp.viewControllers![0] as! CNLiveBaseNavigationController
let fromVC = fromNavi.viewControllers.first as! CNLiveAuthViewController
guard let toNaviVC = transitionContext.viewController(forKey: .to) as? CNSlideBaseNavigationController else { return }
if toNaviVC.topViewController is CNUpSlideDetailViewController{
guard let selectedCell = fromVC.selectedCell else { return }
let toVC = toNaviVC.topViewController as! CNUpSlideDetailViewController
let frame = selectedCell.convert(selectedCell.bgBackView.frame, to: fromVC.view)
//2.Set presentation original size.
toVC.view.frame = frame
toVC.scrollView.imageView.frame.size.width = GlobalConstants.todayCardSize.width
toVC.scrollView.imageView.frame.size.height = GlobalConstants.todayCardSize.height
containerView.addSubview(toNaviVC.view)
//3.Change original size to final size with animation.
UIView.animate(withDuration: transitonDuration, delay: 0, usingSpringWithDamping: 0.5, initialSpringVelocity: 0, options: [], animations: {
toVC.view.frame = UIScreen.main.bounds
toVC.scrollView.imageView.frame.size.width = KSreenWidth
toVC.scrollView.imageView.frame.size.height = KSreenWidth*DownSlideSingleCourseImgH/DownSlideSingleCourseW
toVC.closeBtn.alpha = 1
}) { (completed) in
transitionContext.completeTransition(completed)
}
}else if toNaviVC.topViewController is CNHomepageDuanJuDetailViewController{
guard let selectedCell = fromVC.selectedDuanjuCell else { return }
let toVC = toNaviVC.topViewController as! CNHomepageDuanJuDetailViewController
let frame = selectedCell.convert(selectedCell.totalView.frame, to: fromVC.view)
//2.Set presentation original size.
toVC.view.frame = frame
containerView.addSubview(toNaviVC.view)
//3.Change original size to final size with animation.
UIView.animate(withDuration: transitonDuration, delay: 0, usingSpringWithDamping: 0.5, initialSpringVelocity: 0, options: [], animations: {
toVC.view.frame = UIScreen.main.bounds
toVC.closeBtn.alpha = 1
}) { (completed) in
transitionContext.completeTransition(completed)
}
}else{
return
}
}else if self.cellType == .course{
//1.Get fromVC and toVC
guard let fromVC = transitionContext.viewController(forKey: .from) as? CNUpSlideViewController else { return }
guard let toNaviVC = transitionContext.viewController(forKey: .to) as? CNSlideBaseNavigationController else { return }
guard let selectedCell = fromVC.selectedCell else { return }
let toVC = toNaviVC.topViewController as! CNUpSlideDetailViewController
let frame = selectedCell.convert(selectedCell.bgBackView.frame, to: fromVC.view)
//2.Set presentation original size.
toVC.view.frame = frame
toVC.scrollView.imageView.frame.size.width = GlobalConstants.todayCardSize.width
toVC.scrollView.imageView.frame.size.height = GlobalConstants.todayCardSize.height
containerView.addSubview(toNaviVC.view)
//3.Change original size to final size with animation.
UIView.animate(withDuration: transitonDuration, delay: 0, usingSpringWithDamping: 0.5, initialSpringVelocity: 0, options: [], animations: {
toVC.view.frame = UIScreen.main.bounds
toVC.scrollView.imageView.frame.size.width = KSreenWidth
toVC.scrollView.imageView.frame.size.height = KSreenWidth*DownSlideSingleCourseImgH/DownSlideSingleCourseW
toVC.closeBtn.alpha = 1
}) { (completed) in
transitionContext.completeTransition(completed)
}
}else if self.cellType == .playletToCourse{
//1.Get fromVC and toVC
guard let fromVCtemp = transitionContext.viewController(forKey: .from) as? CNLiveBaseTabBarViewController else { return }
let fromNavi = fromVCtemp.viewControllers![0] as! CNLiveBaseNavigationController
let fromVC = fromNavi.viewControllers.first as! CNLiveAuthViewController
// guard let fromVC = transitionContext.viewController(forKey: .from) as? CNUpSlideViewController else { return }
guard let toNaviVC = transitionContext.viewController(forKey: .to) as? CNSlideBaseNavigationController else { return }
guard let selectedCell = fromVC.selectedCell else { return }
let toVC = toNaviVC.topViewController as! CNUpSlideDetailViewController
let frame = selectedCell.convert(selectedCell.bgBackView.frame, to: fromVC.view)
//2.Set presentation original size.
toVC.view.frame = frame
toVC.scrollView.imageView.frame.size.width = GlobalConstants.todayCardSize.width
toVC.scrollView.imageView.frame.size.height = GlobalConstants.todayCardSize.height
containerView.addSubview(toNaviVC.view)
//3.Change original size to final size with animation.
UIView.animate(withDuration: transitonDuration, delay: 0, usingSpringWithDamping: 0.5, initialSpringVelocity: 0, options: [], animations: {
toVC.view.frame = UIScreen.main.bounds
toVC.scrollView.imageView.frame.size.width = KSreenWidth
toVC.scrollView.imageView.frame.size.height = KSreenWidth*DownSlideSingleCourseImgH/DownSlideSingleCourseW
toVC.closeBtn.alpha = 1
}) { (completed) in
transitionContext.completeTransition(completed)
}
}
}
func animationForDismiss(using transitionContext: UIViewControllerContextTransitioning) {
if self.cellType == .playlet{
guard let fromNaviVC = transitionContext.viewController(forKey: .from) as? CNSlideBaseNavigationController else { return }
guard let toVCTabbar = transitionContext.viewController(forKey: .to) as? CNLiveBaseTabBarViewController else { return }
let toVCNav = toVCTabbar.viewControllers?.first as! CNLiveBaseNavigationController
let toVC = toVCNav.viewControllers.first as! CNLiveAuthViewController
if fromNaviVC.topViewController is CNUpSlideDetailViewController{
guard let selectedCell = toVC.selectedCell else { return }
let fromVC = fromNaviVC.topViewController as! CNUpSlideDetailViewController
fromVC.view.isHidden = true
UIView.animate(withDuration: transitonDuration - 0.8, delay: 0, usingSpringWithDamping: 0.8, initialSpringVelocity: 0, options: [], animations: {
let frame = selectedCell.convert(selectedCell.bgBackView.frame, to: toVC.view)
fromVC.view.frame = frame
fromVC.view.layer.cornerRadius = GlobalConstants.toDayCardCornerRadius
fromVC.scrollView.imageView.frame.size.width = GlobalConstants.todayCardSize.width
fromVC.scrollView.imageView.frame.size.height = GlobalConstants.todayCardSize.height
fromVC.closeBtn.alpha = 0
}) { (completed) in
transitionContext.completeTransition(completed)
}
}else if fromNaviVC.topViewController is CNHomepageDuanJuDetailViewController{
guard let selectedCell = toVC.selectedDuanjuCell else { return }
let fromVC = fromNaviVC.topViewController as! CNHomepageDuanJuDetailViewController
fromVC.view.isHidden = true
UIView.animate(withDuration: transitonDuration - 0.8, delay: 0, usingSpringWithDamping: 0.8, initialSpringVelocity: 0, options: [], animations: {
let frame = selectedCell.convert(selectedCell.totalView.frame, to: toVC.view)
fromVC.view.frame = frame
fromVC.view.layer.cornerRadius = GlobalConstants.toDayCardCornerRadius
fromVC.closeBtn.alpha = 0
fromVC.tableView.alpha = 0
}) { (completed) in
transitionContext.completeTransition(completed)
}
}else{
return
}
}else if self.cellType == .course{
guard let fromNaviVC = transitionContext.viewController(forKey: .from) as? CNSlideBaseNavigationController else { return }
guard let toVC = transitionContext.viewController(forKey: .to) as? CNUpSlideViewController else { return }
guard let selectedCell = toVC.selectedCell else { return }
let fromVC = fromNaviVC.topViewController as! CNUpSlideDetailViewController
fromVC.view.isHidden = true
UIView.animate(withDuration: transitonDuration - 0.8, delay: 0, usingSpringWithDamping: 0.8, initialSpringVelocity: 0, options: [], animations: {
let frame = selectedCell.convert(selectedCell.bgBackView.frame, to: toVC.view)
fromVC.view.frame = frame
fromVC.view.layer.cornerRadius = GlobalConstants.toDayCardCornerRadius
fromVC.scrollView.imageView.frame.size.width = GlobalConstants.todayCardSize.width
fromVC.scrollView.imageView.frame.size.height = GlobalConstants.todayCardSize.height
fromVC.closeBtn.alpha = 0
fromVC.tableView.alpha = 0
}) { (completed) in
transitionContext.completeTransition(completed)
}
}else if self.cellType == .playletToCourse{
guard let fromNaviVC = transitionContext.viewController(forKey: .from) as? CNSlideBaseNavigationController else { return }
guard let toVCTabbar = transitionContext.viewController(forKey: .to) as? CNLiveBaseTabBarViewController else { return }
let toVCNav = toVCTabbar.viewControllers?.first as! CNLiveBaseNavigationController
let toVC = toVCNav.viewControllers.first as! CNLiveAuthViewController
guard let selectedCell = toVC.selectedCell else { return }
if fromNaviVC.topViewController is CNUpSlideDetailViewController{
let fromVC = fromNaviVC.topViewController as! CNUpSlideDetailViewController
fromVC.view.isHidden = true
UIView.animate(withDuration: transitonDuration - 0.8, delay: 0, usingSpringWithDamping: 0.8, initialSpringVelocity: 0, options: [], animations: {
let frame = selectedCell.convert(selectedCell.bgBackView.frame, to: toVC.view)
fromVC.view.frame = frame
fromVC.view.layer.cornerRadius = GlobalConstants.toDayCardCornerRadius
fromVC.scrollView.imageView.frame.size.width = GlobalConstants.todayCardSize.width
fromVC.scrollView.imageView.frame.size.height = GlobalConstants.todayCardSize.height
fromVC.closeBtn.alpha = 0
fromVC.tableView.alpha = 0
}) { (completed) in
transitionContext.completeTransition(completed)
}
}else if fromNaviVC.topViewController is CNLiveShortVideoViewController{
let fromVC = fromNaviVC.topViewController as! CNLiveShortVideoViewController
transitionContext.completeTransition(true)
}else{
print("")
}
}
}
}