暫無描述

AlertViewController.swift 2.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. //
  2. // AlertViewController.swift
  3. // PaiAi
  4. //
  5. // Created by FFIB on 2017/11/21.
  6. // Copyright © 2017年 FFIB. All rights reserved.
  7. //
  8. import UIKit
  9. open class AlertViewController: UIViewController, PresentViewController {
  10. open var animationView: UIView? {
  11. if case let .custom(alertView, _) = style {
  12. return alertView
  13. }
  14. return nil
  15. }
  16. public enum Style {
  17. case alert
  18. case actionSheet
  19. case custom(UIView, PresentAnimatable)
  20. }
  21. open var style: Style {
  22. return _style
  23. }
  24. var _style: Style = .alert
  25. public var animator: PresentAnimatable = AlertAnimator()
  26. public init(style: Style = .alert) {
  27. _style = style
  28. super.init(nibName: nil, bundle: nil)
  29. commonInit()
  30. }
  31. override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) {
  32. super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
  33. commonInit()
  34. }
  35. required public init?(coder aDecoder: NSCoder) {
  36. super.init(coder: aDecoder)
  37. commonInit()
  38. }
  39. override open func viewDidLoad() {
  40. super.viewDidLoad()
  41. view.backgroundColor = UIColor(gray: 52, a: 0)
  42. configurationGestures()
  43. }
  44. fileprivate func commonInit() {
  45. modalPresentationStyle = .custom
  46. setTransitioningDelegate(self)
  47. configurationAnimator()
  48. }
  49. fileprivate func configurationGestures() {
  50. configurationTapGesture()
  51. configurationSwipeGesture()
  52. }
  53. fileprivate func configurationTapGesture() {
  54. let tap = UITapGestureRecognizer(target: self, action: #selector(disappear))
  55. tap.setDelegate(self)
  56. view.addGestureRecognizer(tap)
  57. }
  58. fileprivate func configurationSwipeGesture() {
  59. switch _style {
  60. case .alert, .custom: return
  61. case .actionSheet: break
  62. }
  63. let swipe = UISwipeGestureRecognizer(target: self, action: #selector(disappear))
  64. swipe.setDelegate(self)
  65. swipe.direction = .down
  66. view.addGestureRecognizer(swipe)
  67. }
  68. fileprivate func configurationAnimator() {
  69. animator = style.animator
  70. }
  71. @objc public func disappear() {
  72. dismiss(animated: true, completion: nil)
  73. }
  74. }
  75. fileprivate extension AlertViewController.Style {
  76. var animator: PresentAnimatable {
  77. switch self {
  78. case .alert:
  79. return AlertAnimator()
  80. case .actionSheet:
  81. return ActionSheetAnimator()
  82. case let .custom(_, customAnimator):
  83. return customAnimator
  84. }
  85. }
  86. }