123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109 |
- import UIKit
- open class AlertViewController: UIViewController, PresentViewController {
- open var animationView: UIView? {
- if case let .custom(alertView, _) = style {
- return alertView
- }
- return nil
- }
- public enum Style {
- case alert
- case actionSheet
- case custom(UIView, PresentAnimatable)
- }
- open var style: Style {
- return _style
- }
- var _style: Style = .alert
- public var animator: PresentAnimatable = AlertAnimator()
- public init(style: Style = .alert) {
- _style = style
- super.init(nibName: nil, bundle: nil)
- commonInit()
- }
- override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) {
- super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
- commonInit()
- }
- required public init?(coder aDecoder: NSCoder) {
- super.init(coder: aDecoder)
- commonInit()
- }
- override open func viewDidLoad() {
- super.viewDidLoad()
- view.backgroundColor = UIColor(gray: 52, a: 0)
- configurationGestures()
- }
- fileprivate func commonInit() {
- modalPresentationStyle = .custom
- setTransitioningDelegate(self)
- configurationAnimator()
- }
- fileprivate func configurationGestures() {
- configurationTapGesture()
- configurationSwipeGesture()
- }
- fileprivate func configurationTapGesture() {
- let tap = UITapGestureRecognizer(target: self, action: #selector(disappear))
- tap.setDelegate(self)
- view.addGestureRecognizer(tap)
- }
- fileprivate func configurationSwipeGesture() {
- switch _style {
- case .alert, .custom: return
- case .actionSheet: break
- }
- let swipe = UISwipeGestureRecognizer(target: self, action: #selector(disappear))
- swipe.setDelegate(self)
- swipe.direction = .down
- view.addGestureRecognizer(swipe)
- }
- fileprivate func configurationAnimator() {
- animator = style.animator
- }
- @objc public func disappear() {
- dismiss(animated: true, completion: nil)
- }
- }
- fileprivate extension AlertViewController.Style {
- var animator: PresentAnimatable {
- switch self {
- case .alert:
- return AlertAnimator()
- case .actionSheet:
- return ActionSheetAnimator()
- case let .custom(_, customAnimator):
- return customAnimator
- }
- }
- }
|