forked from jonkykong/SideMenu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SideMenuInteractionController.swift
65 lines (53 loc) · 1.58 KB
/
SideMenuInteractionController.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
//
// SideMenuInteractiveTransitionController.swift
// SideMenu
//
// Created by Jon Kent on 12/28/18.
//
import UIKit
internal final class SideMenuInteractionController: UIPercentDrivenInteractiveTransition {
enum State { case
update(progress: CGFloat),
finish,
cancel
}
private(set) var isCancelled: Bool = false
private(set) var isFinished: Bool = false
init(cancelWhenBackgrounded: Bool = true, completionCurve: UIView.AnimationCurve = .easeIn) {
super.init()
self.completionCurve = completionCurve
guard cancelWhenBackgrounded else { return }
NotificationCenter.default.addObserver(self, selector: #selector(handleNotification), name: UIApplication.didEnterBackgroundNotification, object: nil)
}
override func cancel() {
isCancelled = true
super.cancel()
}
override func finish() {
isFinished = true
super.finish()
}
override func update(_ percentComplete: CGFloat) {
guard !isCancelled && !isFinished else { return }
super.update(percentComplete)
}
func handle(state: State) {
switch state {
case .update(let progress):
update(progress)
case .finish:
finish()
case .cancel:
cancel()
}
}
}
private extension SideMenuInteractionController {
@objc func handleNotification(notification: NSNotification) {
switch notification.name {
case UIApplication.didEnterBackgroundNotification:
cancel()
default: break
}
}
}