-
Notifications
You must be signed in to change notification settings - Fork 3k
/
PhotonActionSheetAnimator.swift
79 lines (64 loc) · 3.49 KB
/
PhotonActionSheetAnimator.swift
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
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
import UIKit
class PhotonActionSheetAnimator: NSObject, UIViewControllerAnimatedTransitioning {
var presenting: Bool = false
let animationDuration = 0.4
lazy var shadow: UIView = {
let shadow = UIView()
shadow.backgroundColor = UIColor(white: 0, alpha: 0.5)
return shadow
}()
func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
let screens = (from: transitionContext.viewController(forKey: .from)!, to: transitionContext.viewController(forKey: .to)!)
guard let actionSheet = (self.presenting ? screens.to : screens.from) as? PhotonActionSheet else {
return
}
let bottomViewController = (self.presenting ? screens.from : screens.to) as UIViewController
animateWitVC(actionSheet, presentingVC: bottomViewController, transitionContext: transitionContext)
}
func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval {
return animationDuration
}
}
extension PhotonActionSheetAnimator: UIViewControllerTransitioningDelegate {
func animationController(forPresented presented: UIViewController, presenting: UIViewController, source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
self.presenting = true
return self
}
func animationController(forDismissed dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
self.presenting = false
return self
}
}
extension PhotonActionSheetAnimator {
fileprivate func animateWitVC(_ actionSheet: PhotonActionSheet, presentingVC viewController: UIViewController, transitionContext: UIViewControllerContextTransitioning) {
let containerView = transitionContext.containerView
if presenting {
shadow.frame = containerView.bounds
containerView.addSubview(shadow)
actionSheet.view.frame = CGRect(origin: CGPoint(x: 0, y: containerView.frame.size.height), size: containerView.frame.size)
self.shadow.alpha = 0
containerView.addSubview(actionSheet.view)
actionSheet.view.layoutIfNeeded()
UIView.animate(withDuration: transitionDuration(using: transitionContext), delay: 0, usingSpringWithDamping: 0.8, initialSpringVelocity: 0.3, options: [], animations: { () -> Void in
self.shadow.alpha = 1
actionSheet.view.frame = containerView.bounds
actionSheet.view.layoutIfNeeded()
}, completion: { (completed) -> Void in
transitionContext.completeTransition(completed)
})
} else {
UIView.animate(withDuration: transitionDuration(using: transitionContext), delay: 0, usingSpringWithDamping: 1.2, initialSpringVelocity: 0.0, options: [], animations: { () -> Void in
self.shadow.alpha = 0
actionSheet.view.frame = CGRect(origin: CGPoint(x: 0, y: containerView.frame.size.height), size: containerView.frame.size)
actionSheet.view.layoutIfNeeded()
}, completion: { (completed) -> Void in
actionSheet.view.removeFromSuperview()
self.shadow.removeFromSuperview()
transitionContext.completeTransition(completed)
})
}
}
}