Created
April 11, 2017 19:41
-
-
Save sauvikatinnofied/97868d427762259c12b20205ca223104 to your computer and use it in GitHub Desktop.
List Load Showing Shimmer View
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
import UIKit | |
class ShimmerView: UIView { | |
var primaryColor: UIColor = UIColor(red: 214.0/255.0, green: 214.0/255.0, blue: 214.0/255.0, alpha: 1.0) | |
var animationColor: UIColor = .white | |
override init(frame: CGRect) { | |
super.init(frame: frame) | |
initialSetUp() | |
} | |
required init?(coder aDecoder: NSCoder) { | |
super.init(coder: aDecoder) | |
initialSetUp() | |
} | |
func initialSetUp() { | |
//backgroundColor = primaryColor | |
//layer.masksToBounds = true | |
//layer.cornerRadius = bounds.height / 2.0 | |
} | |
} | |
class RoundRectView: UIView { | |
override init(frame: CGRect) { | |
super.init(frame: frame) | |
initialSetUp() | |
} | |
required init?(coder aDecoder: NSCoder) { | |
super.init(coder: aDecoder) | |
initialSetUp() | |
} | |
func initialSetUp() { | |
layer.masksToBounds = true | |
layer.cornerRadius = bounds.height / 2.0 | |
} | |
} | |
class Animate { | |
/// Add a persistent shimmer animation. Usage: `Animate.shimmer(myView)` | |
static func startShimming(view: UIView, afterDelay delay: Double) { | |
let gradient = CAGradientLayer() | |
gradient.startPoint = CGPoint(x:0, y:0) | |
gradient.endPoint = CGPoint(x:1, y:0.0) | |
gradient.frame = CGRect(x:0, y:0, width:view.bounds.size.width*3, height:view.bounds.size.height) | |
let lowerAlpha: CGFloat = 0.7 | |
let solid = UIColor(white: 1, alpha: 1).cgColor | |
let clear = UIColor(white: 1, alpha: lowerAlpha).cgColor | |
gradient.colors = [ solid, solid, clear, clear, solid, solid ] | |
gradient.locations = [ 0, 0.3, 0.45, 0.55, 0.7, 1 ] | |
let theAnimation : CABasicAnimation = CABasicAnimation(keyPath: "transform.translation.x") | |
theAnimation.beginTime = CACurrentMediaTime() + delay | |
theAnimation.duration = 0.8 | |
theAnimation.repeatCount = Float.infinity | |
theAnimation.autoreverses = false | |
theAnimation.isRemovedOnCompletion = false | |
theAnimation.fillMode = kCAFillModeForwards | |
theAnimation.fromValue = -view.frame.size.width * 2 | |
theAnimation.toValue = 0 | |
gradient.add(theAnimation, forKey: "animateLayer") | |
view.layer.mask = gradient | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment