I am trying to draw angular gradient using CaGradientLayer. I know angle can be defined using startPoint and endPoint. I can compute these points for some standard angles like 0, 90, 180, 360 etc. But I want formulate these points for arbitrary angle. I have tried computing it using some trigonometry, but didn't get any success. Can anyone give me any directions on how to compute these points for arbitrary angles?
Defining angle of the gradient using CAGradientLayer
12.1k Views Asked by blancos At
3
There are 3 best solutions below
1
On
Swift 3
static func setGradient(view: UIView!,viewRadius: CGFloat!, color1: UIColor!, color2: UIColor!, angle: Double!, alphaValue: CGFloat!){
let gradient = CAGradientLayer()
gradient.frame = CGRect(origin: CGPoint.zero, size: view.frame.size)
gradient.colors = [color1.withAlphaComponent(alphaValue).cgColor, color2.withAlphaComponent(alphaValue).cgColor]
let x: Double! = angle / 360.0
let a = pow(sinf(Float(2.0 * M_PI * ((x + 0.75) / 2.0))),2.0);
let b = pow(sinf(Float(2*M_PI*((x+0.0)/2))),2);
let c = pow(sinf(Float(2*M_PI*((x+0.25)/2))),2);
let d = pow(sinf(Float(2*M_PI*((x+0.5)/2))),2);
gradient.endPoint = CGPoint(x: CGFloat(c),y: CGFloat(d))
gradient.startPoint = CGPoint(x: CGFloat(a),y:CGFloat(b))
view.roundCorners([.topLeft, .bottomLeft], radius: viewRadius)
view.layer.insertSublayer(gradient, at: 0)
}
2
On
This is heavily based on Sarthak Sharma's solution. I made an extension of UIView and I figured the readability and type conversions could be improved a bit:
extension UIView {
func setGradient(colors: [CGColor], angle: Float = 0) {
let gradientLayerView: UIView = UIView(frame: CGRect(x:0, y: 0, width: bounds.width, height: bounds.height))
let gradient: CAGradientLayer = CAGradientLayer()
gradient.frame = gradientLayerView.bounds
gradient.colors = colors
let alpha: Float = angle / 360
let startPointX = powf(
sinf(2 * Float.pi * ((alpha + 0.75) / 2)),
2
)
let startPointY = powf(
sinf(2 * Float.pi * ((alpha + 0) / 2)),
2
)
let endPointX = powf(
sinf(2 * Float.pi * ((alpha + 0.25) / 2)),
2
)
let endPointY = powf(
sinf(2 * Float.pi * ((alpha + 0.5) / 2)),
2
)
gradient.endPoint = CGPoint(x: CGFloat(endPointX),y: CGFloat(endPointY))
gradient.startPoint = CGPoint(x: CGFloat(startPointX), y: CGFloat(startPointY))
gradientLayerView.layer.insertSublayer(gradient, at: 0)
layer.insertSublayer(gradientLayerView.layer, at: 0)
}
}
Add this to a new or a related Swift file and all you have to do is call either myView.setGradient(colors: gradientColorsArray)
or myView.setGradient(colors: gradientColorsArray, angle: 90).
Here's a method that creates a view that allows 360 degree rotation of its two-colour gradient based upon input from a slider (or anything). The incoming slider value ("x" variable below) is between 0.0 and 1.0.
At 0.0 the gradient is horizontal (with colour A on top, and colour B below), rotating through 360 degrees to value 1.0 (identical to value 0.0 - or a full rotation).
E.g. when x = 0.25, colour A is left and colour B is right. At 0.5, colour A is below and colour B is above, 0.75 colour A is right and colour B is left. It rotates anti-clockwise from right to left.
It takes four arguments: frame, colourA, colourB and the input value (0-1).