Вот мой код в Swift, я думаю, он хорошо работает:
func shadowView (view : UIView , r : CGFloat, gr : CGFloat , b : CGFloat , header : Bool)
{
let bottomColor = UIColor (red: r/255 , green: gr/255 , blue: b/255 , alpha: 0)
//var bottomColor = UIColor ( red: (r/255), green : (g/255), blue : (b/255), alpha : 0)
//var topColor = UIColor ( red: (r/255), green : (g/255), blue : (b/255), alpha : 1)
let topColor = UIColor (red: r/255, green: gr/255, blue: b/255, alpha: 1)
var arrayColors: [CGColor] = [
topColor.CGColor,
bottomColor.CGColor
]
if header
{
let headerShadow: CAGradientLayer = CAGradientLayer()
headerShadow.frame = CGRect(x: 0, y: 0, width: UIScreen.mainScreen().bounds.width, height: view.frame.size.height)
headerShadow.colors = arrayColors
view.layer.insertSublayer(headerShadow, atIndex: 1)
} else
{
let footerShadow: CAGradientLayer = CAGradientLayer()
arrayColors = [
bottomColor.CGColor,
topColor.CGColor
]
footerShadow.frame = CGRect(x: 0, y: 0 , width: UIScreen.mainScreen().bounds.width, height: view.frame.size.height)
footerShadow.colors = arrayColors
view.layer.insertSublayer(footerShadow, atIndex: 1)
}
}