Created
June 23, 2017 15:17
-
-
Save fuxingloh/a69ecef7db2db9577f4d605766ba7087 to your computer and use it in GitHub Desktop.
How to add gradient on top and bottom of image 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
// Call this in layoutSubView or viewDidLoad | |
self.imageView.layer.sublayers?.forEach { $0.removeFromSuperlayer() } | |
let width = self.bounds.width | |
let height = self.bounds.height | |
let sHeight:CGFloat = 60.0 | |
let shadow = UIColor.black.withAlphaComponent(0.6).cgColor | |
// Add gradient bar for image on top | |
let topImageGradient = CAGradientLayer() | |
topImageGradient.frame = CGRect(x: 0, y: 0, width: width, height: sHeight) | |
topImageGradient.colors = [shadow, UIColor.clear.cgColor] | |
imageView.layer.insertSublayer(topImageGradient, at: 0) | |
let bottomImageGradient = CAGradientLayer() | |
bottomImageGradient.frame = CGRect(x: 0, y: height - sHeight, width: width, height: sHeight) | |
bottomImageGradient.colors = [UIColor.clear.cgColor, shadow] | |
imageView.layer.insertSublayer(bottomImageGradient, at: 0) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment