36. Quartz 2D之渐变和裁剪

Quartz 2D之渐变和裁剪

渐变分为两种: 线性渐变和径向渐变, 我们将学习使用它:

1. 线性渐变

// MARK: - 绘制渐变
// MARK: 线性渐变
func drawGradient(context: CGContextRef?) {
    // 1. 绘制渐变

    // 创建渐变
    let components: [CGFloat] = [1.0, 0.0, 0.0, 1.0, 1.0, 1.0, 1.0, 1.0]
    let locations: [CGFloat] = [0.0, 1
    let gradient = CGGradientCreateWithColorComponents(CGColorSpaceCreateDeviceRGB(), components, locations, 2)

    // 绘制渐变
    CGContextDrawLinearGradient(context, gradient, CGPoint(x: 0.0, y: 0.0), CGPoint(x: self.frame.size.width, y: 0.0), .DrawsAfterEndLocation)
}

我们在drawRect()中调用它, 然后运行程序:
drawGradient

创建渐变的方法:

@available(iOS 2.0, *)
public func CGGradientCreateWithColorComponents(space: CGColorSpace?, _ components: UnsafePointer<CGFloat>, _ locations: UnsafePointer<CGFloat>, _ count: Int) -> CGGradient?
参数说明:
    colorSpace: 颜色空间
    components: 数组, 每4个一组, 表示一个颜色,如 {r, g, b, a, r, g, b, a}
    location: 表示渐变开始的位置

2. 裁剪

在上面的代码中的创建渐变和绘制渐变之间添加下面的代码

// 渐变的区域裁剪
CGContextClipToRect(context, CGRect(x: 100, y: 100, width: 200, height: 100))

运行程序:
裁剪

我们也可以一次创建多个裁剪区域,使用下面的代码替换上面的裁剪:

let rects = [
    CGRect(x: 0, y: 0, width: 100 , height: 100),
    CGRect(x: 100, y: 100, width: 100, height: 100),
    CGRect(x: 200, y: 0, width: 100, height: 100)
]
CGContextClipToRects(context, rects, 3)

运行程序:

裁剪2

3. 径向渐变

// MARK: 径向渐变
func drawGradient2(context: CGContextRef?) {
    // 创建渐变
    let components: [CGFloat] = [1.0, 0.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0]
    let locations: [CGFloat] = [0.5, 1.0]

    let gradient = CGGradientCreateWithColorComponents(CGColorSpaceCreateDeviceRGB(), components, locations, 2)

    // 绘制渐变
    CGContextDrawRadialGradient(context, gradient, CGPoint(x: self.frame.width/2, y: self.frame.height/2), 0, CGPoint(x: self.frame.width/2, y: self.frame.height/2), 150, .DrawsAfterEndLocation)

}

运行程序:
径向渐变

绘制径向渐变的方法的定义如下:

@available(iOS 2.0, *)
public func CGContextDrawRadialGradient(c: CGContext?, _ gradient: CGGradient?, _ startCenter: CGPoint, _ startRadius: CGFloat, _ endCenter: CGPoint, _ endRadius: CGFloat, _ options: CGGradientDrawingOptions)

参数:
    c: 上下文
    gradient: 渐变
    startCenter: 起始中心点
    startRadius: 起始半径
    endCenter: 截止中心点
    endRadius: 截止半径
    options:渐变的填充方式

明天入职新的公司,新的开始哦! \( ̄︶ ̄)/

4. 全部代码

GradientView.swift:

import UIKit

// 渐变
class GradientView: UIView {

    override init(frame: CGRect) {
        super.init(frame: frame)
        self.backgroundColor = UIColor.whiteColor()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    // Only override drawRect: if you perform custom drawing.
    // An empty implementation adversely affects performance during animation.
    override func drawRect(rect: CGRect) {
        // Drawing code

        let context = UIGraphicsGetCurrentContext()
//        self.drawGradient(context)
        self.drawGradient2(context)
    }

    // MARK: - 绘制渐变
    // MARK: 线性渐变
    func drawGradient(context: CGContextRef?) {
        // 1. 绘制渐变

        // 创建渐变
        let components: [CGFloat] = [1.0, 0.0, 0.0, 1.0, 1.0, 1.0, 1.0, 1.0]
        let locations: [CGFloat] = [0.0, 1.0]
        /**
            参数说明:
                1. colorSpace 颜色空间, rgb
                2. components 数组, 每4个一组, 表示一个颜色 {r, g, b, a, r, g, b, a}
                3. location 表示渐变开始的位置
        */
        let gradient = CGGradientCreateWithColorComponents(CGColorSpaceCreateDeviceRGB(), components, locations, 2)

        // 渐变的区域裁剪
//        CGContextClipToRect(context, CGRect(x: 100, y: 100, width: 200, height: 100))

        let rects = [
            CGRect(x: 0, y: 0, width: 100 , height: 100),
            CGRect(x: 100, y: 100, width: 100, height: 100),
            CGRect(x: 200, y: 0, width: 100, height: 100)
        ]
        CGContextClipToRects(context, rects, 3)

        // 绘制渐变
        CGContextDrawLinearGradient(context, gradient, CGPoint(x: 0.0, y: 0.0), CGPoint(x: self.frame.size.width, y: 0.0), .DrawsAfterEndLocation)
    }

    // MARK: 径向渐变
    func drawGradient2(context: CGContextRef?) {
        // 创建渐变
        let components: [CGFloat] = [1.0, 0.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0]
        let locations: [CGFloat] = [0.0, 1.0]
        /**
         参数说明:
         1. colorSpace 颜色空间, rgb
         2. components 数组, 每4个一组, 表示一个颜色 {r, g, b, a, r, g, b, a}
         3. location 表示渐变开始的位置
         */
        let gradient = CGGradientCreateWithColorComponents(CGColorSpaceCreateDeviceRGB(), components, locations, 2)

        // 绘制渐变
        CGContextDrawRadialGradient(context, gradient, CGPoint(x: self.frame.width/2, y: self.frame.height/2), 0, CGPoint(x: self.frame.width/2, y: self.frame.height/2), 150, .DrawsAfterEndLocation)

    }

}

ViewController.swift:

import UIKit

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.

        let view = GradientView(frame: self.view.bounds)
        self.view.addSubview(view)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值