Skip to content

Instantly share code, notes, and snippets.

@akashsoni01
Forked from policante/UIView+Shimmer.swift
Created June 19, 2019 07:15
Show Gist options
  • Save akashsoni01/10368f93333e781eab17493319471553 to your computer and use it in GitHub Desktop.
Save akashsoni01/10368f93333e781eab17493319471553 to your computer and use it in GitHub Desktop.
Shimmer effect to UIView
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