Guest User

Untitled

a guest
May 24th, 2018
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.55 KB | None | 0 0
  1. // Created by Marlon Monroy on 5/19/18.
  2. // Copyright © 2018 Monroy.io All rights reserved.
  3. //
  4.  
  5. import Foundation
  6. import UIKit
  7. import Dispatch
  8.  
  9. protocol Shimmerable {
  10. func start(count: Int) -> Void
  11. func stop() -> Void
  12. var lightColor: UIColor {get set}
  13. var darkColor: UIColor {get set}
  14. var isShimmering: Bool {get}
  15. }
  16.  
  17. extension UIView: Shimmerable {
  18.  
  19. private struct ShimmerProperties {
  20. static let shimmerKey:String = "io.monroy.shimmer.key"
  21. static var lightColor:CGColor = UIColor.white.withAlphaComponent(0.1).cgColor
  22. static var darkColor:CGColor = UIColor.black.withAlphaComponent(1).cgColor
  23. static var isShimmering:Bool = false
  24. static var gradient:CAGradientLayer = CAGradientLayer()
  25. static var animation:CABasicAnimation = CABasicAnimation(keyPath: "locations")
  26. }
  27. var lightColor: UIColor {
  28. get {
  29. return UIColor(cgColor: ShimmerProperties.lightColor)
  30. }
  31. set {
  32. ShimmerProperties.lightColor = newValue.cgColor
  33. }
  34. }
  35. var darkColor: UIColor {
  36. get {
  37. return UIColor(cgColor: ShimmerProperties.darkColor)
  38. }
  39. set {
  40. ShimmerProperties.darkColor = newValue.cgColor
  41. }
  42. }
  43.  
  44. var isShimmering: Bool {
  45. get {
  46. return ShimmerProperties.isShimmering
  47. }
  48. }
  49.  
  50. func stop() {
  51. guard ShimmerProperties.isShimmering else {return}
  52. self.layer.mask?.removeAnimation(forKey: ShimmerProperties.shimmerKey)
  53. self.layer.mask = nil
  54. ShimmerProperties.isShimmering = false
  55. self.layer.setNeedsDisplay()
  56. }
  57.  
  58. func start(count: Int = 3) {
  59. guard !ShimmerProperties.isShimmering else {return}
  60.  
  61. CATransaction.begin()
  62. CATransaction.setCompletionBlock({
  63. self.stop()
  64. })
  65.  
  66. ShimmerProperties.isShimmering = true
  67.  
  68. ShimmerProperties.gradient.colors = [ShimmerProperties.darkColor, ShimmerProperties.lightColor, ShimmerProperties.darkColor];
  69. ShimmerProperties.gradient.frame = CGRect(x: CGFloat(-2*self.bounds.size.width), y: CGFloat(0.0), width: CGFloat(4*self.bounds.size.width), height: CGFloat(self.bounds.size.height))
  70. ShimmerProperties.gradient.startPoint = CGPoint(x: Double(0.0), y: Double(0.5));
  71. ShimmerProperties.gradient.endPoint = CGPoint(x: Double(1.0), y: Double(0.5));
  72. ShimmerProperties.gradient.locations = [0.4, 0.5, 0.6];
  73.  
  74. ShimmerProperties.animation.duration = 1.0
  75. ShimmerProperties.animation.repeatCount = (count > 0) ? Float(count) : .infinity
  76. ShimmerProperties.animation.fromValue = [0.0, 0.12, 0.3]
  77. ShimmerProperties.animation.toValue = [0.6, 0.86, 1.0]
  78.  
  79. ShimmerProperties.gradient.add(ShimmerProperties.animation, forKey: ShimmerProperties.shimmerKey)
  80. self.layer.mask = ShimmerProperties.gradient;
  81.  
  82. CATransaction.commit()
  83. }
  84. }
Add Comment
Please, Sign In to add comment