123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110 |
- import UIKit
- public protocol AlertAnimatorDelegate: class {
- func getContentView() -> UIView?
- }
- open class AlertViewController: UIViewController, AlertAnimatorDelegate {
-
- public enum Style {
- case alert
- case actionSheet
- }
-
- open var style: Style {
- return .alert
- }
-
- fileprivate var animator: AlertViewAnimatable = AlertAnimator()
-
- 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(red: 52 / 255.0, green: 52 / 255.0, blue: 52 / 255.0, alpha: 0.7)
- configurationGestures()
-
- }
-
- fileprivate func commonInit() {
- modalPresentationStyle = .custom
- transitioningDelegate = self
-
- configurationAnimator()
- }
-
- fileprivate func configurationGestures() {
- configurationTapGesture()
- configurationSwipeGesture()
- }
-
- fileprivate func configurationTapGesture() {
- let tap = UITapGestureRecognizer(target: self, action: #selector(disappear))
- tap.delegate = self
- view.addGestureRecognizer(tap)
- }
-
- fileprivate func configurationSwipeGesture() {
- let swipe = UISwipeGestureRecognizer(target: self, action: #selector(disappear))
- swipe.delegate = self
- swipe.direction = .down
- view.addGestureRecognizer(swipe)
- }
-
- fileprivate func configurationAnimator() {
- switch style {
- case .alert:
- animator = AlertAnimator()
- case .actionSheet:
- animator = ActionSheetAnimator()
- }
- }
- @objc func disappear() {
- dismiss(animated: true, completion: nil)
- }
-
- open func getContentView() -> UIView? {
- return nil
- }
- }
- extension AlertViewController: UIGestureRecognizerDelegate {
- public func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldReceive touch: UITouch) -> Bool {
- if touch.view == view {
- return true
- }
- return false
- }
- }
- extension AlertViewController: UIViewControllerTransitioningDelegate {
- fileprivate func animationController(forDismissed dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
- let transitioning = DisappearAnimatedTransitioning(animator: animator)
- transitioning.delegate = self
- return transitioning
- }
-
- fileprivate func animationController(forPresented presented: UIViewController, presenting: UIViewController, source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
- let transitioning = AppearAnimatedTransitioning(animator: animator)
- transitioning.delegate = self
- return transitioning
- }
- }
|