CNHomePageAnimationTransition.swift
3.88 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
//
// CNHomePageAnimationTransition.swift
// metaCode
//
// Created by zx on 2024/1/5.
//
import Foundation
import UIKit
fileprivate let transitonDuration: TimeInterval = 1.0
class CNHomePageAnimationTransition: NSObject {
let animationType: AnimationType!
init(animationType: AnimationType) {
self.animationType = animationType
super.init()
}
}
extension CNHomePageAnimationTransition: 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
//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? CNHomePageBaseNavigationController else { return }
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
}
}
func animationForDismiss(using transitionContext: UIViewControllerContextTransitioning) {
guard let fromNaviVC = transitionContext.viewController(forKey: .from) as? CNHomePageBaseNavigationController 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 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
}) { (completed) in
transitionContext.completeTransition(completed)
}
}else{
return
}
}
}