Created
December 27, 2018 10:04
-
-
Save JayachandraA/1248545d4d8d8556a4b1ea1bbd729180 to your computer and use it in GitHub Desktop.
gradient layer with angle in swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// how to create a gradient view | |
let gradientLayer = CAGradientLayer() | |
gradientLayer.frame = view.bounds | |
gradientLayer.colors = [UIColor.orange.cgColor, UIColor.white.cgColor,UIColor.green.cgColor] | |
gradientLayer.locations = [0.0, 0.5, 1.0] | |
gradientLayer.apply(angle: 45.0) | |
view.layer.insertSublayer(gradientLayer, at: 0) | |
//helper extention to CAGradientLayer | |
extension CAGradientLayer { | |
func apply(angle : Double) { | |
let x: Double! = angle / 360.0 | |
let a = pow(sinf(Float(2.0 * Double.pi * ((x + 0.75) / 2.0))),2.0); | |
let b = pow(sinf(Float(2*Double.pi*((x+0.0)/2))),2); | |
let c = pow(sinf(Float(2*Double.pi*((x+0.25)/2))),2); | |
let d = pow(sinf(Float(2*Double.pi*((x+0.5)/2))),2); | |
endPoint = CGPoint(x: CGFloat(c),y: CGFloat(d)) | |
startPoint = CGPoint(x: CGFloat(a),y:CGFloat(b)) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
func getPoints(from degrees: Double) -> (UnitPoint, UnitPoint) {
let x = (180 - degrees) / 360
}
works for me when copy angle from figma