我有自定义的集合视图单元格,其中包含ImageViews和标签。我想在图像视图中添加渐变颜色。
我尝试了所有可能的解决方案,如添加蒙版和添加渐变到图像的子层。但是什么都不管用,请指导我解决这个问题
let gradientLayer = CAGradientLayer()
gradientLayer.frame = self.asanasImage.layer.bounds
gradientLayer.colors = [UIColor.clear.cgColor,UIColor.black.cgColor]
gradientLayer.locations = [0.7,1.2]
self.asanasImage.layer.addSublayer(gradientLayer)
发布于 2019-02-19 19:03:00
他!尝试在方法中设置imageView的渐变
cellForItemAt(_ indexpath: IndexPath)
方法与其在集合视图单元格中的相同。
使用此方法
func setGradientBackground(imageView: UIImageView, colorTop: UIColor, colorBottom: UIColor) {
let gradientLayer = CAGradientLayer()
gradientLayer.colors = [colorBottom.cgColor, colorTop.cgColor]
gradientLayer.startPoint = CGPoint(x: 0.5, y: 1.0)
gradientLayer.endPoint = CGPoint(x: 0.5, y: 0.0)
gradientLayer.locations = [0, 1]
gradientLayer.frame = self.view.bounds
imageView.layer.insertSublayer(gradientLayer, at: 0)
}
最后,在cellForItemAt(_ indexpath: IndexPath)
内部设置imageView的渐变,如下所示
setGradientBackground(imageView: cell.your_image_View, colorTop: any_color, colorBottom: any_color)
根据您的需要和您想要的渐变颜色自定义方法。
希望我能帮上忙。如果有效,请让我知道并投票支持!谢谢你:D
https://stackoverflow.com/questions/54760588
复制相似问题