SideMenuInteractionController.swift 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. //
  2. // SideMenuInteractiveTransitionController.swift
  3. // SideMenu
  4. //
  5. // Created by Jon Kent on 12/28/18.
  6. //
  7. import UIKit
  8. internal final class SideMenuInteractionController: UIPercentDrivenInteractiveTransition {
  9. enum State { case
  10. update(progress: CGFloat),
  11. finish,
  12. cancel
  13. }
  14. private(set) var isCancelled: Bool = false
  15. private(set) var isFinished: Bool = false
  16. init(cancelWhenBackgrounded: Bool = true, completionCurve: UIView.AnimationCurve = .easeIn) {
  17. super.init()
  18. self.completionCurve = completionCurve
  19. guard cancelWhenBackgrounded else { return }
  20. NotificationCenter.default.addObserver(self, selector: #selector(handleNotification), name: UIApplication.didEnterBackgroundNotification, object: nil)
  21. }
  22. override func cancel() {
  23. isCancelled = true
  24. super.cancel()
  25. }
  26. override func finish() {
  27. isFinished = true
  28. super.finish()
  29. }
  30. override func update(_ percentComplete: CGFloat) {
  31. guard !isCancelled && !isFinished else { return }
  32. super.update(percentComplete)
  33. }
  34. func handle(state: State) {
  35. switch state {
  36. case .update(let progress):
  37. update(progress)
  38. case .finish:
  39. finish()
  40. case .cancel:
  41. cancel()
  42. }
  43. }
  44. }
  45. private extension SideMenuInteractionController {
  46. @objc func handleNotification(notification: NSNotification) {
  47. switch notification.name {
  48. case UIApplication.didEnterBackgroundNotification:
  49. cancel()
  50. default: break
  51. }
  52. }
  53. }