Guest User

Untitled

a guest
Jan 12th, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.34 KB | None | 0 0
  1. //
  2. // Created by Michał Gorzałczany on 04.01.2018.
  3. // Copyright © 2018 Michał Gorzałczany. All rights reserved.
  4. //
  5.  
  6. import UIKit
  7.  
  8. enum MGAlertActionType {
  9. case Default
  10. case Destructive
  11.  
  12. func textColor() -> UIColor {
  13. switch self {
  14. case .Default: return UIColor.darkBlue()
  15. case .Destructive: return UIColor.red
  16. }
  17. }
  18. }
  19.  
  20. struct MGAlertAction {
  21. let title : String
  22. let type : MGAlertActionType
  23. let handler: (() -> Void)?
  24. let exit : Bool
  25.  
  26. init(title: String? = "",
  27. type: MGAlertActionType = .Default,
  28. shouldExit : Bool = true,
  29. handler: (() -> Void)? = nil) {
  30.  
  31. self.title = title!
  32. self.type = type
  33. self.handler = handler
  34. self.exit = shouldExit
  35. }
  36. }
  37.  
  38. final class MGAlertViewController: UIViewController {
  39.  
  40. private var alertView: UIView!
  41. private var blurView: UIView!
  42. private var titleLabel: UILabel!
  43. private var messageLabel: UILabel!
  44. private var bodyStackView: UIStackView!
  45. private var buttonsStackView: UIStackView!
  46. private var actions = [MGAlertAction]()
  47. private(set) var insideView : UIView?
  48. private(set) var alertTitle : String?
  49. private(set) var alertMessage : String?
  50.  
  51. convenience init(title : String?, message : String?) {
  52. self.init()
  53. alertTitle = title
  54. alertMessage = message
  55. }
  56.  
  57.  
  58. func showAlert(fromViewController: UIViewController? = UIApplication.shared.keyWindow?.rootViewController) {
  59. modalPresentationStyle = .overCurrentContext
  60. modalTransitionStyle = .crossDissolve
  61. fromViewController?.present(self, animated: true, completion: nil)
  62. }
  63.  
  64. override func viewDidLoad() {
  65. super.viewDidLoad()
  66. configureView()
  67. configureActions()
  68. configureInsideView()
  69.  
  70. let selector = #selector(dismiss as () -> Void)
  71. let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: selector)
  72. tap.cancelsTouchesInView = false
  73. blurView.addGestureRecognizer(tap)
  74. }
  75.  
  76. func addInsideView(_ insideView: UIView) {
  77. self.insideView = insideView
  78. }
  79.  
  80. func dismiss(){
  81. self.dismiss(animated: true, completion: nil)
  82. }
  83.  
  84. func dismiss(_ action: MGAlertAction) {
  85. self.dismiss(animated: true) {
  86. action.handler?()
  87. }
  88. }
  89.  
  90. func configureInsideView() {
  91. guard let insideView = self.insideView else {return}
  92. bodyStackView.addArrangedSubview(insideView)
  93. }
  94.  
  95. func addAction(_ action: MGAlertAction) {
  96. actions.append(action)
  97. }
  98.  
  99. func configureActions() {
  100. for action in actions {
  101. let button = ClosureButton(type: .system)
  102. button.setTitle(action.title)
  103. button.backgroundColor = UIColor.white
  104. button.setTitleColor(action.type.textColor(), for: .normal)
  105. button.heightAnchor.constraint(equalToConstant: 45).isActive = true
  106.  
  107. button.didTouchUpInside = { [weak self] _ in
  108. if action.exit {
  109. self?.dismiss(action)
  110. } else {
  111. action.handler?()
  112. }
  113. }
  114.  
  115. buttonsStackView.addArrangedSubview(button)
  116. if buttonsStackView.arrangedSubviews.count > 3 {
  117. buttonsStackView.axis = .vertical
  118. }
  119. }
  120. }
  121.  
  122. func configureView() {
  123. view.backgroundColor = UIColor.clear
  124.  
  125. alertView = UIView()
  126. blurView = UIView()
  127. buttonsStackView = UIStackView()
  128.  
  129. alertView.translatesAutoresizingMaskIntoConstraints = false
  130. blurView.translatesAutoresizingMaskIntoConstraints = false
  131. buttonsStackView.translatesAutoresizingMaskIntoConstraints = false
  132.  
  133. //blur View
  134. view.addSubview(blurView)
  135. view.addConstraints([
  136. NSLayoutConstraint(item: blurView, attribute: .width, relatedBy: .equal, toItem: view, attribute: .width, multiplier: 1, constant: 0),
  137. NSLayoutConstraint(item: blurView, attribute: .height, relatedBy: .equal, toItem: view, attribute: .height, multiplier: 1, constant: 0),
  138. NSLayoutConstraint(item: blurView, attribute: .centerX, relatedBy: .equal, toItem: view, attribute: .centerX, multiplier: 1, constant: 0),
  139. NSLayoutConstraint(item: blurView, attribute: .centerY, relatedBy: .equal, toItem: view, attribute: .centerY, multiplier: 1, constant: 0)
  140. ])
  141.  
  142. //Alert View
  143. alertView.backgroundColor = UIColor.white
  144. alertView.clipsToBounds = true
  145. view.addSubview(alertView)
  146. view.addConstraints([
  147. NSLayoutConstraint(item: alertView, attribute: .width, relatedBy: .equal, toItem: view, attribute: .width, multiplier: 0.8, constant: 0),
  148. NSLayoutConstraint(item: alertView, attribute: .height, relatedBy: .lessThanOrEqual, toItem: view, attribute: .height, multiplier: 0.8, constant: 0),
  149. NSLayoutConstraint(item: alertView, attribute: .centerX, relatedBy: .equal, toItem: view, attribute: .centerX, multiplier: 1, constant: 0),
  150. NSLayoutConstraint(item: alertView, attribute: .centerY, relatedBy: .equal, toItem: view, attribute: .centerY, multiplier: 1, constant: 0)
  151. ])
  152.  
  153.  
  154. //Body Stack View
  155. bodyStackView = UIStackView()
  156. bodyStackView.translatesAutoresizingMaskIntoConstraints = false
  157. bodyStackView.axis = .vertical
  158. alertView.addSubview(bodyStackView)
  159.  
  160. view.addConstraints([
  161. NSLayoutConstraint(item: bodyStackView, attribute: .top, relatedBy: .equal, toItem: alertView, attribute: .top, multiplier: 1, constant: 16),
  162. NSLayoutConstraint(item: bodyStackView, attribute: .trailing, relatedBy: .equal, toItem: alertView, attribute: .trailing, multiplier: 1, constant: -16),
  163. NSLayoutConstraint(item: bodyStackView, attribute: .leading, relatedBy: .equal, toItem: alertView, attribute: .leading, multiplier: 1, constant: 16),
  164. ])
  165.  
  166.  
  167. //Title Label
  168. if let title = alertTitle {
  169. titleLabel = UILabel()
  170. titleLabel.numberOfLines = 0
  171. titleLabel.textAlignment = .center
  172. titleLabel.text = title
  173. bodyStackView.addArrangedSubview(titleLabel)
  174. }
  175.  
  176. //Message Label
  177. if let message = alertMessage {
  178. messageLabel = UILabel()
  179. messageLabel.numberOfLines = 0
  180. messageLabel.textAlignment = .center
  181. messageLabel.text = message
  182. bodyStackView.addArrangedSubview(messageLabel)
  183. }
  184.  
  185. //Buttons Stack View
  186. alertView.addSubview(buttonsStackView)
  187.  
  188. view.addConstraints([
  189. NSLayoutConstraint(item: buttonsStackView, attribute: .top, relatedBy: .equal, toItem: bodyStackView, attribute: .bottom, multiplier: 1, constant: 16),
  190. NSLayoutConstraint(item: buttonsStackView, attribute: .bottom, relatedBy: .equal, toItem: alertView, attribute: .bottom, multiplier: 1, constant: 0),
  191. NSLayoutConstraint(item: buttonsStackView, attribute: .trailing, relatedBy: .equal, toItem: alertView, attribute: .trailing, multiplier: 1, constant: 0),
  192. NSLayoutConstraint(item: buttonsStackView, attribute: .leading, relatedBy: .equal, toItem: alertView, attribute: .leading, multiplier: 1, constant: 0),
  193. ])
  194.  
  195. //stack view spacer
  196. let spacerView = UIView()
  197. spacerView.backgroundColor = UIColor.lightGray
  198. spacerView.translatesAutoresizingMaskIntoConstraints = false
  199. alertView.insertSubview(spacerView, belowSubview: buttonsStackView)
  200.  
  201. view.addConstraints([
  202. NSLayoutConstraint(item: spacerView, attribute: .width, relatedBy: .equal, toItem: buttonsStackView, attribute: .width, multiplier: 1, constant: 0),
  203. NSLayoutConstraint(item: spacerView, attribute: .height, relatedBy: .equal, toItem: buttonsStackView, attribute: .height, multiplier: 1, constant: 0.5),
  204. NSLayoutConstraint(item: spacerView, attribute: .centerX, relatedBy: .equal, toItem: buttonsStackView, attribute: .centerX, multiplier: 1, constant: 0),
  205. NSLayoutConstraint(item: spacerView, attribute: .centerY, relatedBy: .equal, toItem: buttonsStackView, attribute: .centerY, multiplier: 1, constant: -0.5)
  206. ])
  207.  
  208.  
  209. //Settings
  210. alertView.layer.cornerRadius = 0
  211. blurView.backgroundColor = UIColor.darkGray
  212. blurView.alpha = 0.5
  213. buttonsStackView.spacing = 0.5
  214. spacerView.alpha = 0.2
  215. buttonsStackView.layoutMargins = UIEdgeInsetsMake(0, 8, 0, 8)
  216. buttonsStackView.axis = .horizontal
  217. buttonsStackView.distribution = .fillEqually
  218. }
  219. }
  220.  
  221. class ClosureButton: UIButton {
  222.  
  223. typealias DidTapButton = (ClosureButton) -> ()
  224.  
  225. var didTouchUpInside: DidTapButton? {
  226. didSet {
  227. if didTouchUpInside != nil {
  228. addTarget(self, action: #selector(ClosureButton.didTouchUpInside(sender:)), for: .touchUpInside)
  229. } else {
  230. removeTarget(self, action: #selector(ClosureButton.didTouchUpInside(sender:)), for: .touchUpInside)
  231. }
  232. }
  233. }
  234.  
  235. func didTouchUpInside(sender: UIButton) {
  236. if let handler = didTouchUpInside {
  237. handler(self)
  238. }
  239. }
  240. }
Add Comment
Please, Sign In to add comment