-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathActionSheetAnimator.swift
More file actions
53 lines (44 loc) · 1.89 KB
/
ActionSheetAnimator.swift
File metadata and controls
53 lines (44 loc) · 1.89 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
import UIKit
///
/// `ActionSheetAnimator` is used to handle the presentation and dismissal animation of `ActionSheet`
///
final class ActionSheetAnimator: NSObject {
let isPresentation: Bool
init(isPresentation: Bool) {
self.isPresentation = isPresentation
super.init()
}
}
extension ActionSheetAnimator: UIViewControllerAnimatedTransitioning {
func transitionDuration(
using transitionContext: UIViewControllerContextTransitioning?
) -> TimeInterval {
return ActionSheetConstants.animationDuration
}
func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
let key: UITransitionContextViewControllerKey = isPresentation ? .to : .from
guard let controller = transitionContext.viewController(forKey: key)
else { return }
if isPresentation {
transitionContext.containerView.addSubview(controller.view)
}
let presentedFrame = transitionContext.finalFrame(for: controller)
var dismissedFrame = presentedFrame
// adding 50 to offset a weird UIKit bug that doesn't place the view off screen
dismissedFrame.origin.y = transitionContext.containerView.frame.size.height + 50
let initialFrame = isPresentation ? dismissedFrame : presentedFrame
let finalFrame = isPresentation ? presentedFrame : dismissedFrame
let animationDuration = transitionDuration(using: transitionContext)
controller.view.frame = initialFrame
UIView.animate(withDuration: animationDuration,
delay: 0,
options: .curveEaseInOut) {
controller.view.frame = finalFrame
} completion: { finished in
if !self.isPresentation {
controller.view.removeFromSuperview()
}
transitionContext.completeTransition(finished)
}
}
}