Skip to content

Instantly share code, notes, and snippets.

@aybekckaya
Created June 25, 2018 23:52
Show Gist options
  • Save aybekckaya/eae622ab1a7aa4b5bed34e478159daf2 to your computer and use it in GitHub Desktop.
Save aybekckaya/eae622ab1a7aa4b5bed34e478159daf2 to your computer and use it in GitHub Desktop.
Loading view like Facebook
extension UIView {
func animateLoading () {
let gradient = CAGradientLayer()
gradient.startPoint = CGPoint(x: 0, y: 0)
gradient.endPoint = CGPoint(x: 1, y: 0)
gradient.frame = CGRect(x: 0, y: 0, width: self.bounds.size.width*3, height: self.bounds.size.height)
let lowerAlpha: CGFloat = 0.75
let solid = UIColor(white: 1, alpha: 1).cgColor
let clear = UIColor(white: 1, alpha: lowerAlpha).cgColor
gradient.colors = [ clear, clear, clear, solid, solid, solid,solid,clear,clear,clear]
gradient.locations = [ 0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9 ,1 ]
let theAnimation : CABasicAnimation = CABasicAnimation(keyPath: "transform.translation.x")
theAnimation.duration = 1
theAnimation.repeatCount = Float.infinity
theAnimation.autoreverses = false
theAnimation.isRemovedOnCompletion = false
theAnimation.fillMode = kCAFillModeBoth
theAnimation.fromValue = -self.frame.size.width * 2
theAnimation.toValue = 0
gradient.add(theAnimation, forKey: "loaderAnimation")
self.layer.mask = gradient
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment