Created
February 6, 2017 14:09
-
-
Save policante/2e45915e8077635fcb7826958c4aa7bd 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 | |
} | |
} |
Works using gradient.colors = [alpha, light, alpha]
it doesnt work at presented modally viewcontroller, why?
@gali8 Worked! Thanks!
it work with uitableView as well..
gradient.frame = CGRect(x: -self.bounds.size.width, y: 0, width: 3 * self.bounds.size.width, height: self.bounds.size.height)
sometimes when you create your cell using code this line will not work because UIElements do not get frame initially.. please use
gradient.frame = CGRect(x: 0, y: 0, width: (UIScreen.main.bounds.width), height: some static value )
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hello, I have not used this extension in TableView. I used it in an ImageView.
See this library for help:
https://github.com/malkouz/ListPlaceholder