Skip to content

Latest commit

 

History

History
46 lines (35 loc) · 1.58 KB

README.md

File metadata and controls

46 lines (35 loc) · 1.58 KB

CSSGradientToUIKit

Swift 5.0+ Xcode 12.0+ iOS 10.0+ License Platform

The main idea of this converter is adopt the same gradient from Web to iOS. The CSS has linear-gradient() func to create gradient.

Web CSS

background: linear-gradient(-275DEG, red, yellow);
background: linear-gradient(35deg, red, yellow);
background: linear-gradient(680deg, red, yellow);

Unlike UIKit has another coordinate subsystem for CGGradientLayer:

gradientLayer.startPoint = CGPoint(x:0, y:0.559)
gradientLayer.endPoint = CGPoint(x:1, y:0.441)
// and so on

Example of -275 degree

css -275 gradient

Usage

let size = CGSize(width: 200, height: 100)
let gradient = CAGradientLayer()
gradient.frame = CGRect(x: 0, y: 0, width: size.width, height: size.height)
gradient.colors = [UIColor.red.cgColor, UIColor.yellow.cgColor]

let points = gradientPoints(size: gradient.bounds.size, cssAngle: -275)
gradient.startPoint = points.start
gradient.endPoint = points.end

License

CSSGradientToUIKit is released under the MIT License.