-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCustomAlertAnimations.swift
More file actions
65 lines (49 loc) · 2.17 KB
/
CustomAlertAnimations.swift
File metadata and controls
65 lines (49 loc) · 2.17 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
//
// CustomAlertAnimations.swift
//
// Created by Dave Neff
//
import UIKit
// MARK: - Presentation
final class CustomAlertPresentationAnimation: NSObject, UIViewControllerAnimatedTransitioning {
private let duration: TimeInterval = 0.15
func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
guard let toController = transitionContext.viewController(forKey: .to),
let toView = toController.view else { fatalError("Failed to retrieve toController") }
transitionContext.containerView.addSubview(toView)
UIView.animate(
withDuration: duration,
animations: {
/* Presentation animation itself is handled in PresentationController
// This UIView.animate method must be invoked for two reasons:
// 1. Determines the duration of animation
// 2. Tell the transitionContext that the transition has completed */
},
completion: { didComplete in
transitionContext.completeTransition(didComplete)
})
}
func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval {
return duration
}
}
// MARK: - Dismissal
final class CustomAlertDismissalAnimation: NSObject, UIViewControllerAnimatedTransitioning {
private let duration: TimeInterval = 0.2
func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
guard let fromController = transitionContext.viewController(forKey: .from),
let fromView = fromController.view else { fatalError("Failed to retrieve fromController") }
UIView.animate(
withDuration: duration,
animations: {
fromView.alpha = 0.0
},
completion: { didComplete in
fromView.removeFromSuperview()
transitionContext.completeTransition(didComplete)
})
}
func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval {
return duration
}
}