Gradient defined by trigonometric function in the main chart

Apple made it very easy to make linear and radial gradients, but is it possible for the gradient color to be specified by the defined function? In my situation, I want the fill color of the object to change using the sine function along the x axis. It's easy to make png and use them as templates instead, but I'm just wondering if gradients can be made where the red, green, and blue components change along a specific axis using a sine function.

Any answer is welcome. Thanks in advance.

+4
source share
1 answer

CAGradientLayer, colors . , , , , ( ) .

, -, .

// Compute the colors using a sine step-size
let samples = 100
var colors = [CGColor]()
for i in 0..<samples {
    let component = CGFloat(0.5 + sin(Double(i) / Double(samples - 1) * 4.0 * M_PI) / 2.0)
    colors.append(UIColor(red: component, green: 0, blue: 1 - component, alpha: 1).CGColor)
}

// Create the gradient layer
let gradientLayer: CAGradientLayer = CAGradientLayer()
gradientLayer.colors = colors

// Install the gradient layer        
gradientLayer.frame = self.view.bounds
self.view.layer.insertSublayer(gradientLayer, atIndex: 0)

.

Xcode simulator screenshot

-1

All Articles