Guest User

Untitled

a guest
Jun 18th, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.95 KB | None | 0 0
  1. # Shimmering Effect
  2.  
  3. ```swift
  4. import UIKit
  5.  
  6. extension UIView {
  7. func startShimmering() {
  8. let light = self.tintColor.cgColor
  9. let dark = UIColor(white: 0, alpha: 0.3).cgColor
  10.  
  11. let gradient = CAGradientLayer()
  12. gradient.colors = [light, dark, light]
  13. gradient.frame = CGRect(x: -self.bounds.size.width, y: 0, width: 3 * self.bounds.width, height: self.bounds.height)
  14. gradient.location = [0.4, 0.5, 0.6]
  15. self.layer.mask = gradient
  16.  
  17. let animation = CABasicAnimation(keyPath: "location")
  18. animation.fromValue = [0.0, 0.1, 0.2]
  19. animation.toValue = [0.8, 0.9, 1.0]
  20.  
  21. animation.duration = 1.5
  22. animation.repeatCount = HUGE
  23.  
  24. gradient.add(animation, forKey: "shimmer")
  25. }
  26.  
  27. func stopShimmering() {
  28. self.layer.mask = nil
  29. }
  30.  
  31. func resumeShimmering() {
  32. guard let gradientMask = self.layer.mask as? CAGradientLayer else { return }
  33. gradientMask.removeAnimation(forKey: "shimmer")
  34. self.startShimmering()
  35. }
  36.  
  37. }
  38. ```
Add Comment
Please, Sign In to add comment