实现的效果图:
网上所谓的渐变大多数的都是界面滑动时导航栏透明度的改变,特此开一篇文章出来做记录,共同学习
其实渐变色很简单,利用CAGradientLayer即可。直接上代码:
letgradientLayer =CAGradientLayer()
let sizeLength = self.navigationController?.navigationBar.frame.width
letframeAndStatusBar =CGRect(x:0, y:0, width: sizeLength!, height:64)
gradientLayer.frame= frameAndStatusBar
gradientLayer.startPoint=CGPoint(x:0, y:0)
gradientLayer.endPoint=CGPoint(x:1, y:0)
gradientLayer.colors = [UIColor.blue.cgColor, UIColor.red.cgColor]
navigationController?.navigationBar.setBackgroundImage(LayerImage(fromLayer: gradientLayer), for: .default)
LayerImage函数如下:
func LayerImage(fromLayer layer:CALayer) ->UIImage{
UIGraphicsBeginImageContext(layer.frame.size)
layer.render(in: UIGraphicsGetCurrentContext()!)
let outputImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
returnoutputImage!
}
网友评论