Няма описание

FFAlertController.swift 7.1KB

    // // FFAlertController.swift // FFAlert // // Created by FFIB on 2017/11/13. // Copyright © 2017年 FFIB. All rights reserved. // import UIKit public enum FFAlertStyle { case actionSheet case alert } public class FFAlertController: UIViewController { // private private var transitionDelegate: FFTransitioning private var alert: UIView & FFAlertRepresentable public var alertStyle: FFAlertStyle public var contentEdgeInsets: UIEdgeInsets { get { return self.alert.contentEdgInsets } set { alert.contentEdgInsets = newValue } } public var attributedTitle: NSAttributedString? { get { return self.alert.title } set { self.alert.title = newValue } } public var attributedMessage: NSAttributedString? { get { return self.alert.message } set { self.alert.message = newValue } } public override var title: String? { get { return attributedTitle?.string } set { attributedTitle = NSAttributedString(string: newValue ?? "", attributes: [NSAttributedString.Key.font: UIFont.systemFont(ofSize: 17), NSAttributedString.Key.foregroundColor: UIColor.init(red: 0.2, green: 0.2, blue: 0.2, alpha: 1.0)]) } } public var message: String? { get { return attributedMessage?.string } set { attributedMessage = NSAttributedString(string: newValue ?? "", attributes: [NSAttributedString.Key.font: UIFont.systemFont(ofSize: 14), NSAttributedString.Key.foregroundColor: UIColor.init(red: 0.6, green: 0.6, blue: 0.6, alpha: 1.0)]) } } private var alertActions: [FFAlertAction] { get { return alert.alertActions } set { alert.alertActions = newValue } } public var alertEdgInsets: (left: CGFloat, right: CGFloat) { get { return alert.edgInsets } set { alert.edgInsets = newValue } } convenience init(title: String? = nil, message: String? = nil, customView: UIView, alertStyle: FFAlertStyle = .alert) { self.init(alertStyle: alertStyle) self.alert.contentView = customView self.title = title self.message = message } convenience init(title: String? = nil, message: String? = nil, alertStyle: FFAlertStyle = .alert) { self.init(alertStyle: alertStyle) self.title = title self.message = message } convenience init(attributedTitle: NSAttributedString? = nil, attributedMessage: NSAttributedString? = nil, alertStyle: FFAlertStyle = .alert) { self.init(alertStyle: alertStyle) self.attributedTitle = attributedTitle self.attributedMessage = attributedMessage } private init(alertStyle: FFAlertStyle) { switch alertStyle { case .alert: self.alert = FFAlertView() case .actionSheet: self.alert = FFSheetView() } self.alertStyle = alertStyle self.transitionDelegate = FFTransitioning(alertStyle: alertStyle) super.init(nibName: nil, bundle: nil) commonInit() } required public init?(coder aDecoder: NSCoder) { fatalError("init(coder:) has not been implemented") } private func commonInit() { self.modalPresentationStyle = .custom self.transitioningDelegate = self.transitionDelegate } override public var preferredStatusBarStyle: UIStatusBarStyle { return presentingViewController?.preferredStatusBarStyle ?? .default } override public var supportedInterfaceOrientations: UIInterfaceOrientationMask { return presentingViewController?.supportedInterfaceOrientations ?? super.supportedInterfaceOrientations } override public func viewDidLoad() { super.viewDidLoad() configurationOfAlertView() let tap = UITapGestureRecognizer(target: self, action: #selector(disappear)) tap.delegate = self view.addGestureRecognizer(tap) } @objc func disappear() { dismiss(animated: true, completion: nil) } func configurationOfAlertView() { view.addSubview(alert) alert.translatesAutoresizingMaskIntoConstraints = false alert.backgroundColor = UIColor.white switch alertStyle { case .actionSheet: NSLayoutConstraint.activate( [NSLayoutConstraint(item: alert, attribute: .leading, relatedBy: .equal, toItem: view, attribute: .leading, multiplier: 1, constant: alertEdgInsets.left), NSLayoutConstraint(item: alert, attribute: .bottom, relatedBy: .equal, toItem: view, attribute: .bottom, multiplier: 1, constant: 0), NSLayoutConstraint(item: alert, attribute: .trailing, relatedBy: .equal, toItem: view, attribute: .trailing, multiplier: 1, constant: -alertEdgInsets.right), NSLayoutConstraint(item: alert, attribute: .height, relatedBy: .lessThanOrEqual, toItem: view, attribute: .height, multiplier: 1, constant: 0)] ) alert.setContentCompressionResistancePriority(.required, for: .vertical) case .alert: NSLayoutConstraint.activate( [NSLayoutConstraint(item: alert, attribute: .leading, relatedBy: .equal, toItem: view, attribute: .leading, multiplier: 1, constant: alertEdgInsets.left), NSLayoutConstraint(item: alert, attribute: .centerY, relatedBy: .equal, toItem: view, attribute: .centerY, multiplier: 1, constant: 0), NSLayoutConstraint(item: alert, attribute: .trailing, relatedBy: .equal, toItem: view, attribute: .trailing, multiplier: 1, constant: -alertEdgInsets.right), NSLayoutConstraint(item: alert, attribute: .height, relatedBy: .lessThanOrEqual, toItem: view, attribute: .height, multiplier: 1, constant: 0)] ) alert.setContentCompressionResistancePriority(.required, for: .vertical) } } func addAlertAction(alertAction: FFAlertAction) { alertActions.append(alertAction) } } extension FFAlertController: UIGestureRecognizerDelegate { public func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldReceive touch: UITouch) -> Bool { if alert.frame.contains(touch.location(in: view)) { return false } return true } }