Guest User

Untitled

a guest
Nov 17th, 2017
322
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.50 KB | None | 0 0
  1. //
  2. // LoadingView.swift
  3. //
  4. // Created by Eduardo Sanches Bocato on 15/11/17.
  5. //
  6.  
  7. import UIKit
  8. import SnapKit
  9.  
  10. // MARK: Constants
  11. fileprivate struct Defaults {
  12. static let backgroundViewAlpha: CGFloat = 0.9
  13. static let activityIndicatorViewSquareSize = 30
  14. static let animationDuration = 0.35
  15. }
  16.  
  17. class LoadingView: UIControl {
  18.  
  19. // MARK: View Elements
  20. private var backgroundView: UIView!
  21. private var activityIndicatorView: UIActivityIndicatorView!
  22.  
  23. // MARK: Properties
  24. private var showBackgroundView: Bool? {
  25. willSet {
  26. if let newValue = newValue, newValue != showBackgroundView {
  27. self.backgroundColor = newValue ? UIColor.clear : UIColor.white
  28. self.backgroundView.backgroundColor = newValue ? UIColor.white : UIColor.clear
  29. self.backgroundView.layer.borderColor = newValue ? UIColor.groupTableViewBackground.cgColor : UIColor.clear.cgColor
  30. self.backgroundView.layoutSubviews()
  31. }
  32. }
  33. }
  34. private var backGroundViewSquareSize: Int {
  35. return 100
  36. }
  37.  
  38. // MARK: - Initialization
  39. required init?(coder aDecoder: NSCoder) {
  40. super.init(coder: aDecoder)
  41. }
  42.  
  43. private init(frame: CGRect, activityIndicatorStyle style: UIActivityIndicatorViewStyle, activityIndicatorColor color: UIColor = UIColor.lightGray) {
  44. super.init(frame: frame)
  45. self.configureViewElements(activityIndicatorStyle: style, activityIndicatorColor: color)
  46. }
  47.  
  48. // MARK: - Instantiation
  49. fileprivate static func instantiateNew(in view: UIView!, withActivityIndicatorStyle style: UIActivityIndicatorViewStyle, activityIndicatorColor color: UIColor = UIColor.lightGray) -> LoadingView? {
  50. let loadingView = LoadingView(frame: view.frame, activityIndicatorStyle: style, activityIndicatorColor: color)
  51. view.addSubview(loadingView)
  52. loadingView.snp.makeConstraints { (make) in
  53. make.left.right.bottom.top.equalTo(view)
  54. }
  55. return loadingView
  56. }
  57.  
  58. fileprivate static func instantiateNew(in context: Any!, withActivityIndicatorStyle style: UIActivityIndicatorViewStyle, activityIndicatorColor color: UIColor = UIColor.lightGray) -> LoadingView? {
  59.  
  60. if let contextAsController = context as? UIViewController {
  61. return self.instantiateNew(in: contextAsController.view, withActivityIndicatorStyle: style, activityIndicatorColor: color)
  62. }
  63. else if let contextAsView = context as? UIView {
  64. return self.instantiateNew(in: contextAsView, withActivityIndicatorStyle: style, activityIndicatorColor: color)
  65. }
  66.  
  67. return nil
  68. }
  69.  
  70. // MARK: - Layout Configuration
  71. private func configureViewElements(activityIndicatorStyle style: UIActivityIndicatorViewStyle = .whiteLarge, activityIndicatorColor color: UIColor = UIColor.lightGray) {
  72. self.backgroundColor = UIColor.white
  73. self.isHidden = true
  74. self.configureActivityIndicatorBackgroundView()
  75. self.configureActivityIndicator(activityIndicatorStyle: style, activityIndicatorColor: color)
  76. }
  77.  
  78. fileprivate func configureActivityIndicatorBackgroundView() {
  79. self.backgroundView = UIView()
  80. self.backgroundView.backgroundColor = UIColor.white
  81. self.backgroundView.alpha = Defaults.backgroundViewAlpha
  82. self.backgroundView.layer.cornerRadius = 10.0
  83. self.backgroundView.layer.borderColor = UIColor.clear.cgColor
  84. self.backgroundView.layer.borderWidth = 2.0
  85. self.addSubview(self.backgroundView)
  86. self.backgroundView.snp.makeConstraints { (make) in
  87. make.centerX.centerY.equalTo(self)
  88. make.height.width.equalTo(self.backGroundViewSquareSize)
  89. }
  90. }
  91.  
  92. fileprivate func configureActivityIndicator(activityIndicatorStyle style: UIActivityIndicatorViewStyle = .whiteLarge, activityIndicatorColor color: UIColor = UIColor.lightGray) {
  93. self.activityIndicatorView = UIActivityIndicatorView(activityIndicatorStyle: style)
  94. self.activityIndicatorView.translatesAutoresizingMaskIntoConstraints = false
  95. self.activityIndicatorView.color = color
  96. self.backgroundView?.addSubview(self.activityIndicatorView)
  97. self.activityIndicatorView.snp.makeConstraints { (make) in
  98. make.centerX.centerY.equalTo(self.backgroundView)
  99. make.height.width.equalTo(Defaults.activityIndicatorViewSquareSize)
  100. }
  101. }
  102.  
  103. // MARK: Constraints
  104. fileprivate func remakeConstraints() {
  105. self.backgroundView.snp.remakeConstraints { (make) in
  106. make.centerX.centerY.equalTo(self)
  107. make.height.width.equalTo(self.backGroundViewSquareSize)
  108. }
  109. self.activityIndicatorView.snp.remakeConstraints { (make) in
  110. make.top.equalTo(self.backgroundView.snp.top).offset(20)
  111. make.centerY.lessThanOrEqualTo(self.backgroundView)
  112. make.centerX.equalTo(self.backgroundView)
  113. make.height.width.equalTo(Defaults.activityIndicatorViewSquareSize)
  114. }
  115. }
  116.  
  117. // MARK: - Public Actions
  118. public static func hide(for context: Any!) {
  119. if let contextAsController = context as? UIViewController {
  120. self.hide(forContext: contextAsController.view)
  121. }
  122. else if let contextAsView = context as? UIView {
  123. self.hide(forContext: contextAsView)
  124. }
  125. }
  126.  
  127. fileprivate static func hide(forContext view: UIView?){
  128. if let viewSubviews = view?.subviews, let currentVisibleLoadingView = viewSubviews.filter({ $0.isKind(of: LoadingView.self) }).first as? LoadingView {
  129. currentVisibleLoadingView.hide()
  130. currentVisibleLoadingView.removeFromSuperview()
  131. }
  132. }
  133.  
  134. public static func show(in context: Any!, withText text: String? = nil, background: Bool = false, activityIndicatorStyle style: UIActivityIndicatorViewStyle = .whiteLarge, activityIndicatorColor color: UIColor? = nil) {
  135. if let loadingView = LoadingView.instantiateNew(in: context, withActivityIndicatorStyle: style) {
  136. loadingView.show(withBackground: background)
  137. }
  138. }
  139.  
  140. // MARK: - Behavior
  141. fileprivate func hide() {
  142. self.showBackgroundView = false
  143. self.alpha = 1.0
  144. self.backgroundView.alpha = Defaults.backgroundViewAlpha
  145. UIView.animate(withDuration: Defaults.animationDuration, animations: {
  146. self.alpha = 0.0
  147. }, completion: { (finished) in
  148. self.activityIndicatorView.stopAnimating()
  149. self.backgroundView.isHidden = true
  150. self.isHidden = true
  151. })
  152. }
  153.  
  154. fileprivate func show(withBackground background: Bool = false) {
  155. self.showBackgroundView = background
  156. self.backgroundView.isHidden = !background
  157. self.backgroundView.alpha = 0.0
  158. self.isHidden = false
  159. self.alpha = 0.0
  160. self.activityIndicatorView.startAnimating()
  161. UIView.animate(withDuration: Defaults.animationDuration) {
  162. self.alpha = 1.0
  163. self.backgroundView.alpha = Defaults.backgroundViewAlpha
  164. }
  165. }
  166.  
  167. }
Add Comment
Please, Sign In to add comment