Guest User

Untitled

a guest
Feb 17th, 2019
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.72 KB | None | 0 0
  1. import UIKit
  2.  
  3. var overalyView: UIView = UIView()
  4. var activityIndicatorOverlay: UIView = UIView()
  5. var activityIndicator: UIActivityIndicatorView = UIActivityIndicatorView()
  6.  
  7. // =================================================================================================
  8. // API
  9. // =================================================================================================
  10. extension UIViewController {
  11. func showActivityIndicatorOverlay() {
  12. setupOverlay()
  13. setupActivityIndicatorOverlay()
  14. setupActivityIndicator()
  15. stackViews()
  16. activityIndicator.startAnimating()
  17. }
  18.  
  19. // remove activity indicator from this view
  20. func hideActivityIndicatorOverlay() {
  21. activityIndicator.stopAnimating()
  22. activityIndicatorOverlay.removeFromSuperview()
  23. }
  24. }
  25.  
  26. // =================================================================================================
  27. // Internal Methods
  28. // ================================================================================================
  29. extension UIViewController {
  30. struct Colors {
  31. static let white = UIColorFromHex(rgbValue: 0xffffff, alpha: 0.3)
  32. static let dark = UIColorFromHex(rgbValue: 0x444444, alpha: 0.7)
  33. }
  34.  
  35. private func setupOverlay() {
  36. overalyView.frame = self.view.frame
  37. overalyView.center = self.view.center
  38. overalyView.backgroundColor = Colors.dark
  39. }
  40.  
  41. private func setupActivityIndicatorOverlay() {
  42. activityIndicatorOverlay.frame = CGRect.init(x: 0, y: 0, width: 80, height: 80)
  43. activityIndicatorOverlay.center = self.view.center
  44. activityIndicatorOverlay.backgroundColor = Colors.dark
  45. activityIndicatorOverlay.clipsToBounds = true
  46. activityIndicatorOverlay.layer.cornerRadius = 10
  47. }
  48.  
  49. private func setupActivityIndicator() {
  50. activityIndicator.frame = CGRect.init(x: 0.0, y: 0.0, width: 40.0, height: 40.0)
  51. activityIndicator.style = UIActivityIndicatorView.Style.whiteLarge
  52. activityIndicator.center = CGPoint.init(x: activityIndicatorOverlay.frame.size.width / 2, y: activityIndicatorOverlay.frame.size.height / 2)
  53. }
  54.  
  55. private func stackViews() {
  56. activityIndicatorOverlay.addSubview(activityIndicator)
  57. overalyView.addSubview(activityIndicatorOverlay)
  58. self.view.addSubview(overalyView)
  59. }
  60.  
  61. //Define UIColor from hex value
  62. private static func UIColorFromHex(rgbValue:UInt32, alpha:Double=1.0)->UIColor {
  63. let red = CGFloat((rgbValue & 0xFF0000) >> 16)/256.0
  64. let green = CGFloat((rgbValue & 0xFF00) >> 8)/256.0
  65. let blue = CGFloat(rgbValue & 0xFF)/256.0
  66. return UIColor(red:red, green:green, blue:blue, alpha:CGFloat(alpha))
  67. }
  68.  
  69.  
  70. }
Add Comment
Please, Sign In to add comment