-
-
Save akashsoni01/10368f93333e781eab17493319471553 to your computer and use it in GitHub Desktop.
Shimmer effect to UIView
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extension UIView { | |
func startShimmering(){ | |
let light = UIColor.white.cgColor | |
let alpha = UIColor.white.withAlphaComponent(0.7).cgColor | |
let gradient = CAGradientLayer() | |
gradient.colors = [alpha, light, alpha, alpha, light, alpha] | |
gradient.frame = CGRect(x: -self.bounds.size.width, y: 0, width: 3 * self.bounds.size.width, height: self.bounds.size.height) | |
gradient.startPoint = CGPoint(x: 0.0, y: 0.5) | |
gradient.endPoint = CGPoint(x: 1.0, y: 0.525) | |
gradient.locations = [0.4, 0.5, 0.6] | |
self.layer.mask = gradient | |
let animation = CABasicAnimation(keyPath: "locations") | |
animation.fromValue = [0.0, 0.1, 0.2] | |
animation.toValue = [0.8, 0.9, 1.0] | |
animation.duration = 1.5 | |
animation.repeatCount = HUGE | |
gradient.add(animation, forKey: "shimmer") | |
} | |
func stopShimmering(){ | |
self.layer.mask = nil | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment