|
//
// AlertViewController.swift
// PaiAi
//
// Created by FFIB on 2017/11/21.
// Copyright © 2017年 yb. All rights reserved.
//
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
}
}
}
|