iOS 视图实现渐变色背景

打印 上一主题 下一主题

主题 996|帖子 996|积分 2988

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

x
需求

目标是要实现视图的自定义的渐变背景色,实现一个可以或许随时使用的工具。
实现讨论

在 iOS 中,如果设置视图单一的背景色,是很简单的。可是,如果要设置渐变的背景色,该怎么实现呢?其实也没有很是麻烦,其中使用到了 CAGradientLayer 类,只要设置好 CAGradientLayer 对象的属性,然后添加到指定视图即可。
CAGradientLayer 类



  • locations: 指定每一个渐变段的结束
  • startPoint:对应的是第一个渐变结束的点(指的是指定的颜色中,第一个颜色渐变制止的位置)
  • endPoint:对应的是最后一个渐变结束的点(指的是指定的颜色中,最后一个颜色中渐变制止的位置)
  • colors:  指定每个渐变区间的颜色
关于 locations 代码解释文档如下:
  1.     /* An optional array of NSNumber objects defining the location of each
  2.      * gradient stop as a value in the range [0,1]. The values must be
  3.      * monotonically increasing. If a nil array is given, the stops are
  4.      * assumed to spread uniformly across the [0,1] range. When rendered,
  5.      * the colors are mapped to the output colorspace before being
  6.      * interpolated. Defaults to nil. Animatable. */
  7.    
  8.     open var locations: [NSNumber]?
复制代码
locations 中的每一个值,指定的是在渐变方向上每一个渐变制止的位置。默认为空,如果为空,则为匀称渐变。数组的大小不必和渐变区间的数量相同。
渐变的方向就是两个点毗连后指向的方向。
留意这里的坐标:[0, 0] 代表的是图层的左上角,[1, 1] 代表的算是右下角。苹果的代码文档中说的是错误的,解释中的是 macOS 的坐标系统,stackoverflow 上的相关问题 https://stackoverflow.com/questions/20387803/how-to-change-cagradientlayer-color-points
  1.     /* The start and end points of the gradient when drawn into the layer's
  2.      * coordinate space. The start point corresponds to the first gradient
  3.      * stop, the end point to the last gradient stop. Both points are
  4.      * defined in a unit coordinate space that is then mapped to the
  5.      * layer's bounds rectangle when drawn. (I.e. [0,0] is the bottom-left
  6.      * corner of the layer, [1,1] is the top-right corner.) The default values
  7.      * are [.5,0] and [.5,1] respectively. Both are animatable. */
  8.    
  9.     open var startPoint: CGPoint
  10.     open var endPoint: CGPoint
复制代码
示例

1 右下方向渐变

  1. startPoint = CGPoint(x: 0, y: 0)
  2. endPoint = CGPoint(x: 0.5, y: 0.5)
  3. colors = [UIColor.green.cgColor, UIColor.red.cgColor]
复制代码
绿色是从左上角 (0, 0) 开始渐变,红色是截止到 (0.5, 0.5) 不再渐变,右下部分的红色都是纯红色的

2. 整个视图从左上角到右下角渐变

  1. startPoint = CGPoint(x: 0, y: 0)
  2. endPoint = CGPoint(x: 1, y: 1)
  3. colors = [UIColor.green.cgColor, UIColor.red.cgColor]
复制代码



3. 调换颜色渐变方向

  1. startPoint = CGPoint(x: 1, y: 1)
  2. endPoint = CGPoint(x: 0, y: 0)
  3. colors = [UIColor.green.cgColor, UIColor.red.cgColor]
复制代码


4. 垂直渐变

  1. startPoint = CGPoint(x: 0, y: 0)
  2. endPoint = CGPoint(x: 0, y: 1)
  3. colors = [UIColor.green.cgColor, UIColor.red.cgColor]
复制代码

代码

  1.     /*
  2.      * 设置视图渐变背景色
  3.      *
  4.      * @param view 要设置的视图
  5.      * @param frame 区域大小
  6.      * @param colors 渐变颜色数组
  7.      * @param horizontal 渐变方向
  8.      * @param cornerRadius 圆角大小
  9.      *
  10.      */
  11.     public static func setGradientBackgroundColor(view: UIView, frame: CGRect, colors: [CGColor], horizontal: Bool, cornerRadius: CGFloat = 0.0) -> CAGradientLayer {
  12.         let startPoint = CGPoint.init(x: 0.0, y: 0.0)
  13.         var endPoint = CGPoint.init(x: 1.0, y: 0.0)
  14.         if horizontal == false {
  15.             endPoint = CGPoint.init(x: 0.0, y: 1.0)
  16.         }
  17.         
  18.         // 根据颜色的个数生成 locatios
  19.         var locations: [NSNumber] = [NSNumber]()
  20.         let interval = 1.0 / (Double(colors.count) - 1)
  21.         for (index, _) in colors.enumerated() {
  22.             locations.append(interval * Double(index) as NSNumber)
  23.         }
  24.         
  25.         let gradientLayer: CAGradientLayer = getGradientLayer(frame: frame, startPoint: startPoint, endPoint: endPoint, locations: locations, colors: colors)
  26.         
  27.         gradientLayer.zPosition = -10000
  28.         gradientLayer.cornerRadius = cornerRadius
  29.         
  30.         view.layer.addSublayer(gradientLayer)
  31.         
  32.         return gradientLayer
  33.     }
  34.     /**
  35.      *
  36.      * 获取一个颜色渐变层
  37.      *
  38.      * @param frame 大小
  39.      * @param startPoint 颜色渐变起点
  40.      * @param endPoint 颜色渐变终点
  41.      * @param locations 颜色数组对应的点
  42.      * @param colors 渐变颜色数组
  43.      *
  44.      * @return 颜色渐变层
  45.      *
  46.      */
  47.     public static func getGradientLayer(frame: CGRect, startPoint: CGPoint, endPoint: CGPoint, locations: [NSNumber], colors: [CGColor]) -> CAGradientLayer {
  48.         let gradientLayer = CAGradientLayer.init()
  49.         
  50.         gradientLayer.frame = frame
  51.         gradientLayer.startPoint = startPoint
  52.         gradientLayer.endPoint = endPoint
  53.         gradientLayer.locations = locations
  54.         gradientLayer.colors = colors
  55.         
  56.         return gradientLayer
  57.     }
复制代码


免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

不到断气不罢休

金牌会员
这个人很懒什么都没写!
快速回复 返回顶部 返回列表