Newer
Older
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
import UIKit
import Shared
final class SideMenuPresentTransition: NSObject, UIViewControllerAnimatedTransitioning {
static let fromViewTag = UUID().hashValue
init(
dismissInteractor: SideMenuDismissInteracting,
menuAnimator: SideMenuAnimating,
viewAnimator: UIViewAnimating.Type
) {
self.dismissInteractor = dismissInteractor
self.menuAnimator = menuAnimator
self.viewAnimator = viewAnimator
super.init()
}
let dismissInteractor: SideMenuDismissInteracting
let menuAnimator: SideMenuAnimating
let viewAnimator: UIViewAnimating.Type
// MARK: UIViewControllerAnimatedTransitioning
func transitionDuration(using context: UIViewControllerContextTransitioning?) -> TimeInterval { 0.25 }
func animateTransition(using context: UIViewControllerContextTransitioning) {
guard let fromVC = context.viewController(forKey: .from),
let fromSnapshot = fromVC.view.snapshotView(afterScreenUpdates: true),
let toVC = context.viewController(forKey: .to)
else {
context.completeTransition(false)
return
}
context.containerView.addSubview(toVC.view)
toVC.view.frame = context.containerView.bounds
let fromView = UIView()
fromView.tag = Self.fromViewTag
context.containerView.addSubview(fromView)
fromView.frame = context.containerView.bounds
fromView.layer.shadowColor = UIColor.black.cgColor
fromView.layer.shadowOpacity = 1
fromView.layer.shadowOffset = .zero
fromView.layer.shadowRadius = 32
fromView.addSubview(fromSnapshot)
fromSnapshot.frame = fromView.bounds
fromSnapshot.layer.cornerRadius = 0
fromSnapshot.layer.masksToBounds = true
dismissInteractor.setup(
view: fromView,
action: { fromVC.dismiss(animated: true) }
)
viewAnimator.animate(
withDuration: transitionDuration(using: context),
animations: {
self.menuAnimator.animate(in: context.containerView, to: 1)
},
completion: { _ in
let isCancelled = context.transitionWasCancelled
context.completeTransition(isCancelled == false)
}
)
}
}