CALayer-CAGradientLayer(梯度图层)

作者: SSBun | 来源:发表于2016-06-16 18:58 被阅读6334次

** CAGradientlayer可以绘制一个充满整个图层的颜色梯度(包括原型图层等图层)在一个背景颜色上 **

在了解CAGradientLayer之前,我们先要了解一下CALayer的坐标,如下图,一个Layer的左上角为(0,0),其右下角坐标为(1,1),中心点是(0.5,0.5),任何图层都是如此,和父图层以及自身的形状无关.

CALayer坐标

属性

  • colors
    var colors: [AnyObject]?
    一个内部是CGColorRef的数组,规定所有的梯度所显示的颜色,默认为nil

  • locations
    var locations: [NSNumber]?
    一个内部是NSNumber的可选数组,规定所有的颜色梯度的区间范围,选值只能在0到1之间,并且数组的数据必须单增,默认值为nil

  • endPoint
    var endPoint: CGPoint
    图层颜色绘制的终点坐标,也就是阶梯图层绘制的结束点,默认值是(0.5,1.0)

  • startPoint
    var startPoint: CGPoint
    endPoint相互对应,就是绘制阶梯图层的起点坐标,绘制颜色的起点,默认值是(0.5,0.0)

  • type
    var type:String
    绘制类型,默认值是kCAGradientLayerAxial,也就是线性绘制,各个颜色阶层直接的变化是线性的


实例

通过属性大家都差不多知道该如何使用阶梯图层了,接下来我们看一下普通的例子,然后讨论几种情况

//
//  ViewController.swift
//  CALayer-CAGradientLayer
//
//  Created by 蔡士林 on 6/16/16.
//  Copyright © 2016 BZ. All rights reserved.
//

import UIKit

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        // 创建阶梯图层
        let gradientLayer = CAGradientLayer()
        // 设置阶梯图层的背景
        //gradientLayer.backgroundColor = UIColor.grayColor().CGColor
        // 图层的颜色空间(阶梯显示时按照数组的顺序显示渐进色)
        gradientLayer.colors = [UIColor.redColor().CGColor,UIColor.blueColor().CGColor,UIColor.greenColor().CGColor]
        // 各个阶梯的区间百分比
        gradientLayer.locations = [0.1,0.6,1]
        gradientLayer.frame = CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.width)
        gradientLayer.position = self.view.center
        // 绘图的起点(默认是(0.5,0))
        gradientLayer.startPoint = CGPointMake(1, 0)
        // 绘图的终点(默认是(0.5,1))
![Uploading (0,0.5)(1,0.5)@2x_114834.png . . .]

        gradientLayer.endPoint = CGPointMake(0, 1)
        self.view.layer.addSublayer(gradientLayer)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

}

系统默认的绘制方向,从上往下的

![Uploading (0.5,0)(0.5,1)@2x_161190.png . . .]

这是起点为(0,0.5),终点(1,0.5),时横向绘制

(0,0.5)(1,0.5)@2x.png

以起点和终点所画出来的直线做中心轴绘制

(1,0)(0,1)@2x.png

动画

CAGradientLayer的所有属性都能产生隐式动画我们可以通过NSTimer来定时修改Location(其他的也可以(⊙o⊙)哦)

CAGradientLayer.gif
//
//  ViewController.swift
//  CALayer-CAGradientLayer
//
//  Created by 蔡士林 on 6/16/16.
//  Copyright © 2016 BZ. All rights reserved.
//

import UIKit

class ViewController: UIViewController {
    
    var graLayer:CAGradientLayer!
    var index = 0.1

    override func viewDidLoad() {
        super.viewDidLoad()
        
        let gradientLayer = CAGradientLayer()
        gradientLayer.backgroundColor = UIColor.grayColor().CGColor
        gradientLayer.colors = [UIColor.redColor().CGColor,UIColor.blueColor().CGColor,UIColor.greenColor().CGColor]
        gradientLayer.locations = [0.1,0.15,1]
        gradientLayer.frame = CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.width)
        gradientLayer.position = self.view.center
        gradientLayer.startPoint = CGPointMake(1, 0)
        gradientLayer.endPoint = CGPointMake(0, 1)
        self.graLayer = gradientLayer
        self.view.layer.addSublayer(gradientLayer)
        
        
        
    }
    
    override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
        NSTimer.scheduledTimerWithTimeInterval(0.02, target: self, selector: #selector(ViewController.change), userInfo: nil, repeats: true)
    }
    
    func change(){
        index = index + 0.01
        let twoIndex = index + 0.05
        graLayer.locations = [index,twoIndex,1]
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

}

当然了你也可以设置它的其他属性来实现各种各样的动画,你可以充分发挥你的想象力~

有帮助的希望大家点个❤,也是对我的一点点鼓励~

相关文章

  • CALayer-CAGradientLayer(梯度图层)

    ** CAGradientlayer可以绘制一个充满整个图层的颜色梯度(包括原型图层等图层)在一个背景颜色上 **...

  • CAGradient梯度图层

    说明:此文不断更改内容和形式,如有不足请指正. 两个动画效果来了解一下CALayer的两个重要的subClass:...

  • 使用到的一些简单的不常用的方法

    1、给图层添加渐变颜色 colors 一个内部是CGColorRef的数组,规定所有的梯度所显示的颜色,默认为ni...

  • 深入浅出--梯度下降法及其实现

    梯度下降的场景假设梯度梯度下降算法的数学解释梯度下降算法的实例梯度下降算法的实现Further reading 本...

  • 梯度下降和梯度上升

    梯度上升与梯度下降 - HIT-security - 博客园 不管梯度下降还是梯度上升,随着迭代的进行,梯度都是在...

  • (三)线性回归--梯度下降

    一、梯度下降 二、代码的实现 (一.梯度下降) 导包 构建数据 梯度下降 使用梯度下降,可视化 (二。梯度下降矩阵...

  • 24、其他形态学操作

    基本梯度 内外梯度

  • 梯度消失以及梯度爆炸

    GoogLeNet的提出主要就是为了应对梯度消失以及梯度爆炸,本文就是讲解下什么是梯度消失,以及梯度爆炸。 梯度消...

  • 神经网络优化2

    梯度下降 梯度下降法 批梯度下降法(Batch Gradient Descent,BGD)是最常用的梯度下降形式,...

  • 机器学习-常用优化方法

    一阶方法:梯度下降、随机梯度下降、mini 随机梯度下降降法。 随机梯度下降不但速度上比原始梯度下降要快,局部最优...

网友评论

  • 阿杰的人生路:/* The start and end points of the gradient when drawn into the layer's
    * coordinate space. The start point corresponds to the first gradient
    * stop, the end point to the last gradient stop. Both points are
    * defined in a unit coordinate space that is then mapped to the
    * layer's bounds rectangle when drawn. (I.e. [0,0] is the bottom-left
    * corner of the layer, [1,1] is the top-right corner.) The default values
    * are [.5,0] and [.5,1] respectively. Both are animatable. */
  • sun6boys:坐标系统错了。

    /* The start and end points of the gradient when drawn into the layer's
    * coordinate space. The start point corresponds to the first gradient
    * stop, the end point to the last gradient stop. Both points are
    * defined in a unit coordinate space that is then mapped to the
    * layer's bounds rectangle when drawn. (I.e. [0,0] is the bottom-left
    * corner of the layer, [1,1] is the top-right corner.) The default values
    * are [.5,0] and [.5,1] respectively. Both are animatable. */
  • 出门右转掘金见:请问如果要画渐变的线必须要用.mask属性并再加一个图层吗.不能直接用这个layer层画吗

本文标题:CALayer-CAGradientLayer(梯度图层)

本文链接:https://www.haomeiwen.com/subject/vgbmdttx.html