如何从图像视图过渡到颜色?

问题描述:

我想知道如何从图像视图转换到另一种颜色,以便它看起来像图像视图“流”到Swift 3中的背景颜色。我尝试使用渐变视图,但它看起来并不像好样下面的图片:如何从图像视图过渡到颜色?

Picture with image view and background color

+0

你是说你想要的形象图动画,改变画面的色彩鲜艳成黑白?我很困惑,你说*从图像视图转换到另一种颜色* –

+0

不,我很抱歉,很难用另一种语言解释。你看,在图片底部的背景颜色是白色,顶部是图片视图。而我想要的就是从图像渐变为彩色,就像渐变视图一样。 – user8206035

您需要在您的图像视图的顶部添加一个渐变层。渐变从透明转换为白色,以便在图像视图的底部有蓝色效果。这里我有红色

enter image description here

的代码是这样的图像图。此代码创建一个覆盖整个图像视图的图层,并从上到下执行渐变。如果您希望它从屏幕的一半开始,请参阅带注释的行以设置图层的框架。你只需要像做frame.y = y + height/2

let layer = CAGradientLayer() 
layer.frame = self.imageView.bounds// Change this line. 
layer.colors = [ 
    UIColor.white.cgColor, 
    UIColor.init(red: 1, green: 1, blue: 1, alpha: 0).cgColor 
] 
self.imageView.layer.mask = layer 

输出看起来像这样

enter image description here