答案 0 :(得分:0)
确定无论如何我都会发布链接答案的快速版本,类似于您的要求。
你可以这样做,只需创建一个颜色的渐变图层,你想要在渐变层上展开,最后将渐变设置为图像视图,例如
@IBOutlet weak var aImageView: UIImageView!
var theColor: UIColor?
var gradientLayer:CAGradientLayer?
override func viewDidLoad()
{
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
self.theColor = UIColor(colorLiteralRed: 146.0/255.0, green: 146.0/255.0, blue: 146.0/255.0, alpha: 1)
self.gradientLayer = CAGradientLayer()
self.gradientLayer?.frame = self.aImageView.frame
let array: [AnyObject] = [(theColor?.colorWithAlphaComponent(0.1))!.CGColor,
(theColor?.colorWithAlphaComponent(0.12))!.CGColor,(theColor?.colorWithAlphaComponent(0.13))!.CGColor,
(theColor?.colorWithAlphaComponent(0.74))!.CGColor,
(theColor?.colorWithAlphaComponent(0.82))!.CGColor,
(theColor?.colorWithAlphaComponent(1.0))!.CGColor]
gradientLayer?.colors = array;
aImageView.layer .addSublayer(gradientLayer!);
}
只需尝试输入示例项目,结果如下