如何使用倾斜渐变填充CAShapeLayer

use*_*509 15 optimization cashapelayer ios uibezierpath swift

如何CAShapeLayer()使用渐变和45度角填充?

例如,在图像1中,下面的代码绘制一个正方形并填充图层blue(UIColor.blueColor().CGColor).

但是,如何在图像2(即UIColor.blueColor().CGColorUIColor.redColor().CGColor)中填充从蓝色到红色的45度角的渐变?

码:

let path = UIBezierPath()
path.moveToPoint(CGPoint(x: 0, y: 0))
path.addLineToPoint(CGPoint(x: 0, y: 100))
path.addLineToPoint(CGPoint(x: 100, y: 100))
path.addLineToPoint(CGPoint(x: 100, y: 0))
path.closePath()

let shape = CAShapeLayer()
shape.path = path.CGPath
shape.fillColor = UIColor.blueColor().CGColor
Run Code Online (Sandbox Code Playgroud)

正方形旁边的纯色方形,带渐变色

Ali*_*tra 30

为什么不使用CAGradientLayer哪个startPointendPoint属性.

你可以做:

import UIKit
import PlaygroundSupport

let frame = CGRect(x: 0, y: 0, width: 100, height: 100)
let view = UIView(frame: frame)

PlaygroundPage.current.liveView = view

let path = UIBezierPath(ovalIn: frame)

let shape = CAShapeLayer()
shape.frame = frame
shape.path = path.cgPath
shape.fillColor = UIColor.blue.cgColor

let gradient = CAGradientLayer()
gradient.frame = frame
gradient.colors = [UIColor.blue.cgColor,
                   UIColor.red.cgColor]
gradient.startPoint = CGPoint(x: 0, y: 1)
gradient.endPoint = CGPoint(x: 1, y: 0)
gradient.mask = shape

view.layer.addSublayer(gradient)
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

注意:为圆圈添加了贝塞尔曲线路径,因为它可以在没有正方形掩码的情况下工作.


Noa*_*der 7

轻松将渐变应用于CALayer

Swift 4.2,Xcode 10.0

尽管上述解决方案仅在像45°这样的平凡角度真正起作用,但是我的代码能够将梯度设置为任何给定角度。

public extension CALayer {

    public func applyGradient(of colors: UIColor..., atAngle angle: CGFloat) -> CAGradientLayer {
        let gradient = CAGradientLayer()
        gradient.frame = frame
        gradient.colors = colors
        gradient.calculatePoints(for: angle)
        gradient.mask = self
        return gradient
    }

}


public extension CAGradientLayer {

    /// Sets the start and end points on a gradient layer for a given angle.
    ///
    /// - Important:
    /// *0°* is a horizontal gradient from left to right.
    ///
    /// With a positive input, the rotational direction is clockwise.
    ///
    ///    * An input of *400°* will have the same output as an input of *40°*
    ///
    /// With a negative input, the rotational direction is clockwise.
    ///
    ///    * An input of *-15°* will have the same output as *345°*
    ///
    /// - Parameters:
    ///     - angle: The angle of the gradient.
    ///
    public func calculatePoints(for angle: CGFloat) {


        var ang = (-angle).truncatingRemainder(dividingBy: 360)

        if ang < 0 { ang = 360 + ang }

        let n: CGFloat = 0.5

        let tanx: (CGFloat) -> CGFloat = { tan($0 * CGFloat.pi / 180) }

        switch ang {

        case 0...45, 315...360:
            let a = CGPoint(x: 0, y: n * tanx(ang) + n)
            let b = CGPoint(x: 1, y: n * tanx(-ang) + n)
            startPoint = a
            endPoint = b

        case 45...135:
            let a = CGPoint(x: n * tanx(ang - 90) + n, y: 1)
            let b = CGPoint(x: n * tanx(-ang - 90) + n, y: 0)
            startPoint = a
            endPoint = b

        case 135...225:
            let a = CGPoint(x: 1, y: n * tanx(-ang) + n)
            let b = CGPoint(x: 0, y: n * tanx(ang) + n)
            startPoint = a
            endPoint = b

        case 225...315:
            let a = CGPoint(x: n * tanx(-ang - 90) + n, y: 0)
            let b = CGPoint(x: n * tanx(ang - 90) + n, y: 1)
            startPoint = a
            endPoint = b

        default:
            let a = CGPoint(x: 0, y: n)
            let b = CGPoint(x: 1, y: n)
            startPoint = a
            endPoint = b

        }
    }

}
Run Code Online (Sandbox Code Playgroud)

用法:

let layer = CAShapeLayer()

// Setup layer...

// Gradient Direction: ?
let gradientLayer1 = layer.applyGradient(of: UIColor.yellow, UIColor.red, at: 0)

// Gradient Direction: ??
let gradientLayer2 = layer.applyGradient(of: UIColor.purple, UIColor.yellow, UIColor.green, at: -45)

// Gradient Direction: ?
let gradientLayer3 = layer.applyGradient(of: UIColor.yellow, UIColor.blue, UIColor.green, at: 180)

// Gradient Direction: ?
let gradientLayer4 = layer.applyGradient(of: UIColor.red, UIColor.blue, at: 450)
Run Code Online (Sandbox Code Playgroud)

数学解释

因此,我最近实际上花了很多时间自己回答这个问题。以下是一些示例角度,仅用于帮助理解和可视化顺时针旋转方向。

角度示例

如果你有兴趣我怎么想通了,我做了一个表来形象化基本上就是我从做 - 360°

表