Guest User

Untitled

a guest
Dec 10th, 2018
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.80 KB | None | 0 0
  1. //
  2. // Extensions.swift
  3. // PriceBar
  4. //
  5. // Created by Leonid Nifantyev on 1/4/18.
  6. // Copyright © 2018 LionLife. All rights reserved.
  7. //
  8.  
  9. import Foundation
  10. import UIKit
  11.  
  12. typealias ActionClousure = () -> Void
  13.  
  14. extension UITableView {
  15. func update(completion: ActionClousure) {
  16. self.beginUpdates()
  17. completion()
  18. self.endUpdates()
  19. }
  20. }
  21.  
  22. extension UIViewController {
  23. func alert(title: String = "Oh, something goes wrong 😢",
  24. message: String,
  25. okAction: ActionClousure? = nil,
  26. cancelAction: ActionClousure? = nil,
  27. completion: ActionClousure? = nil) {
  28.  
  29. let alert = UIAlertController(title: title, message: message, preferredStyle: UIAlertController.Style.alert)
  30. alert.addAction(UIAlertAction(title: "Ok", style: UIAlertAction.Style.default, handler: { (action: UIAlertAction!) in
  31. okAction?()
  32. }))
  33.  
  34. if let cancelAction = cancelAction {
  35. alert.addAction(UIAlertAction(title: "Not nesasery 🤠",
  36. style: UIAlertAction.Style.cancel,
  37. handler: { (action: UIAlertAction!) in
  38. cancelAction()
  39. }))
  40. }
  41. self.present(alert, animated: true, completion: completion)
  42. }
  43. }
  44.  
  45. extension UIViewController: UITextFieldDelegate {
  46. func addToolBar(textField: UITextField) {
  47. let toolBar = UIToolbar()
  48. toolBar.barStyle = .default
  49. toolBar.isTranslucent = true
  50. toolBar.tintColor = Color.atlantis
  51. let doneButton = UIBarButtonItem(title: "Done", style: .done, target: self, action: #selector(donePressed))
  52. let cancelButton = UIBarButtonItem(title: "Next", style: .plain, target: self, action: #selector(cancelPressed))
  53. let spaceButton = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action: nil)
  54. toolBar.setItems([cancelButton, spaceButton, doneButton], animated: false)
  55. toolBar.isUserInteractionEnabled = true
  56. toolBar.sizeToFit()
  57. textField.delegate = self
  58. textField.inputAccessoryView = toolBar
  59. }
  60.  
  61. @objc func donePressed() {
  62. view.endEditing(true)
  63. }
  64.  
  65. @objc func cancelPressed() {
  66. view.endEditing(true) // or do something
  67. }
  68. }
  69.  
  70. // Activity Indicator
  71. extension UIView {
  72. private var activityIndicatorTag: Int { return 999998 }
  73. private var activityBackgroundTag: Int { return 999997 }
  74.  
  75. public func pb_startActivityIndicator(with title: String) {
  76. DispatchQueue.main.async {
  77. self.startCustomActivityIndicator(with: title, location: self.center)
  78. }
  79. }
  80. func pb_stopActivityIndicator() {
  81. DispatchQueue.main.async {
  82. guard let window = UIApplication.shared.keyWindow else { return }
  83. let views = window.subviews
  84. UIView.animate(withDuration: 0.8, animations: {
  85. for view in views {
  86. if let activityIndicator = view.subviews.filter({ $0.tag == self.activityIndicatorTag }).first as? UIActivityIndicatorView {
  87. guard let superV = activityIndicator.superview else {
  88. fatalError("superview if indicator is not found")
  89. }
  90. activityIndicator.stopAnimating()
  91. activityIndicator.removeFromSuperview()
  92. superV.removeFromSuperview()
  93. break
  94. }
  95. }
  96. })
  97. }
  98. }
  99.  
  100. func startCustomActivityIndicator(with title: String, style: UIActivityIndicatorView.Style = UIActivityIndicatorView.Style.white, location: CGPoint?) {
  101. let locationOnView = location ?? self.center
  102. DispatchQueue.main.async {
  103. let backgroundView = UIView(frame: self.frame)
  104. self.setupLabel(on: backgroundView, title: title, and: locationOnView)
  105. self.setupIndicator(on: backgroundView, with: style, and: location)
  106. self.show(backgroundView)
  107. }
  108. }
  109. func startCustomActivityIndicator(style: UIActivityIndicatorView.Style = UIActivityIndicatorView.Style.white, location: CGPoint?) {
  110. let width = UIScreen.main.bounds.width
  111. let height = UIScreen.main.bounds.width
  112. DispatchQueue.main.async {
  113. let backgroundView = UIView(frame: CGRect(x: 0, y: 0, width: width, height: height))
  114. self.setupIndicator(on: backgroundView, with: style, and: location)
  115. self.show(backgroundView)
  116. }
  117. }
  118.  
  119. private func setupIndicator(on backgroundView: UIView, with style: UIActivityIndicatorView.Style, and location: CGPoint?) {
  120. let locationOnView = location ?? self.center
  121. backgroundView.tag = self.activityBackgroundTag
  122. let activityIndicator = UIActivityIndicatorView(style: style)
  123. activityIndicator.tag = self.activityIndicatorTag
  124. activityIndicator.center = locationOnView
  125. activityIndicator.hidesWhenStopped = true
  126. activityIndicator.startAnimating()
  127. backgroundView.addSubview(activityIndicator)
  128. }
  129.  
  130. private func setupLabel(on view: UIView, title: String, and location: CGPoint) {
  131. let width = UIScreen.main.bounds.width
  132. let titleLabel = UILabel(frame: CGRect(x: 0, y: 0, width: width, height: 100))
  133. titleLabel.textColor = .white
  134. titleLabel.text = title
  135. titleLabel.numberOfLines = 2
  136. titleLabel.textAlignment = .center
  137. titleLabel.center = location
  138. let centerY = titleLabel.center.y
  139. titleLabel.center.y = centerY + 32
  140. view.addSubview(titleLabel)
  141. }
  142.  
  143. func show(_ indicatorView: UIView) {
  144. guard let window = UIApplication.shared.keyWindow else { return }
  145. indicatorView.backgroundColor = .clear
  146. window.addSubview(indicatorView)
  147. UIView.animate(withDuration: 0.8, animations: {
  148. indicatorView.backgroundColor = UIColor.black.withAlphaComponent(0.7)
  149. })
  150. }
  151.  
  152. func obscure() {
  153. guard let window = UIApplication.shared.keyWindow else { return }
  154. self.backgroundColor = .clear
  155. window.addSubview(self)
  156. UIView.animate(withDuration: 0.3, animations: {
  157. self.backgroundColor = UIColor.black.withAlphaComponent(0.7)
  158. })
  159. }
  160.  
  161. func antiObscure(completion: @escaping () -> Void) {
  162. guard let window = UIApplication.shared.keyWindow else { return }
  163. self.backgroundColor = UIColor.black.withAlphaComponent(0.7)
  164. window.addSubview(self)
  165. UIView.animate(withDuration: 0.2, animations: {
  166. self.backgroundColor = .clear
  167. }) { _ in
  168. completion()
  169. }
  170. }
  171. }
  172.  
  173. extension UIButton {
  174. func setEnable(_ enable: Bool) {
  175. DispatchQueue.main.async {
  176. self.isUserInteractionEnabled = enable
  177. self.alpha = enable ? 1 : 0.5
  178. }
  179. }
  180. }
Add Comment
Please, Sign In to add comment