Нет описания

AlertViewController.swift 2.5KB

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