UIImageView某些部分的渐变效果 [英] Gradient effect on some portion of UIImageView

查看:164
本文介绍了UIImageView某些部分的渐变效果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个要求在UIImageView的某些部分(仅在左侧和右侧)添加渐变. 我知道,这可以通过使用CAGradientLayer()来完成.但是我对属性的位置和颜色感到困惑.

I have a requirements of adding gradient over some portion (only in left and right side ) of UIImageView. I know, this can be done by using CAGradientLayer(). But I am confused with properties locations and colors.

下面是我的代码.它在顶部和底部添加了渐变.

Below is my code. It is adding gradient at top and bottom.

 if let containerView = imageView.superView {
        let gradient = CAGradientLayer(layer: containerView.layer)
        gradient.frame = containerView.bounds
        gradient.colors = [UIColor.clear.cgColor, UIColor.blue.cgColor ,UIColor.blue.cgColor, UIColor.clear.cgColor]
        gradient.locations = [0.0 ,  0.25 , 0.85 , 1.0]
        containerView.layer.mask = gradient
    }

预先感谢

推荐答案

您需要像这样并在任意"视图中创建CAGradientLayer

You need to create CAGradientLayer layer like this and in Any view

let gradientLayer = CAGradientLayer()
gradientLayer.frame = CGRectMake(0, 0,300, 380)
gradientLayer.colors = [UIColor(red:55.0/255, green:149.0/255, blue:227.0/255, alpha:1.0).CGColor,UIColor(red:20.0/255, green:77.0/255, blue:120.0/255, alpha:1.0).CGColor]
gradientLayer.startPoint = CGPoint(x: 0.0, y: 1.0)
gradientLayer.endPoint = CGPoint(x: 1.0, y: 1.0)
imgView.layer.insertSublayer(gradientLayer, atIndex: 0)

对于所有方向,例如top-buttomleft-right,反之亦然,只需更改startPointendPoint 上面代码的输出是

For All direction like top-buttom,left-right and vice versa just you need to change startPoint And endPoint Output of above code is

这篇关于UIImageView某些部分的渐变效果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆