Nessuna descrizione

AlertViewController.swift 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. public protocol AlertAnimatorDelegate: class {
  10. func getContentView() -> UIView?
  11. }
  12. open class AlertViewController: UIViewController, AlertAnimatorDelegate {
  13. public enum Style {
  14. case alert
  15. case actionSheet
  16. }
  17. open var style: Style {
  18. return .alert
  19. }
  20. fileprivate var animator: AlertViewAnimatable = AlertAnimator()
  21. override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) {
  22. super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
  23. commonInit()
  24. }
  25. required public init?(coder aDecoder: NSCoder) {
  26. super.init(coder: aDecoder)
  27. commonInit()
  28. }
  29. override open func viewDidLoad() {
  30. super.viewDidLoad()
  31. view.backgroundColor = UIColor(red: 52 / 255.0, green: 52 / 255.0, blue: 52 / 255.0, alpha: 0.7)
  32. configurationGestures()
  33. }
  34. fileprivate func commonInit() {
  35. modalPresentationStyle = .custom
  36. transitioningDelegate = self
  37. configurationAnimator()
  38. }
  39. fileprivate func configurationGestures() {
  40. configurationTapGesture()
  41. configurationSwipeGesture()
  42. }
  43. fileprivate func configurationTapGesture() {
  44. let tap = UITapGestureRecognizer(target: self, action: #selector(disappear))
  45. tap.delegate = self
  46. view.addGestureRecognizer(tap)
  47. }
  48. fileprivate func configurationSwipeGesture() {
  49. let swipe = UISwipeGestureRecognizer(target: self, action: #selector(disappear))
  50. swipe.delegate = self
  51. swipe.direction = .down
  52. view.addGestureRecognizer(swipe)
  53. }
  54. fileprivate func configurationAnimator() {
  55. switch style {
  56. case .alert:
  57. animator = AlertAnimator()
  58. case .actionSheet:
  59. animator = ActionSheetAnimator()
  60. }
  61. }
  62. @objc func disappear() {
  63. dismiss(animated: true, completion: nil)
  64. }
  65. open func getContentView() -> UIView? {
  66. return nil
  67. }
  68. }
  69. extension AlertViewController: UIGestureRecognizerDelegate {
  70. public func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldReceive touch: UITouch) -> Bool {
  71. if touch.view == view {
  72. return true
  73. }
  74. return false
  75. }
  76. }
  77. extension AlertViewController: UIViewControllerTransitioningDelegate {
  78. fileprivate func animationController(forDismissed dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
  79. let transitioning = DisappearAnimatedTransitioning(animator: animator)
  80. transitioning.delegate = self
  81. return transitioning
  82. }
  83. fileprivate func animationController(forPresented presented: UIViewController, presenting: UIViewController, source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
  84. let transitioning = AppearAnimatedTransitioning(animator: animator)
  85. transitioning.delegate = self
  86. return transitioning
  87. }
  88. }