Skip to content

CSS func linear-gradient() to UIKit CGGradientLayer coordinate space conversion

License

Notifications You must be signed in to change notification settings

nikolay-kapustin/CSSGradientToUIKit

Repository files navigation

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.

About

CSS func linear-gradient() to UIKit CGGradientLayer coordinate space conversion

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages