UIGradient 1.3.2

UIGradient 1.3.2

Dinh Quang Hieu 维护。



  • 作者:
  • Đinh Quang Hiếu



Platform Swift 5 Version License

安装

UIGradient现在可在CocoaPods上使用。只需将其添加到项目的Podfile中,然后您就可以继续下一步了。

pod 'UIGradient'

使用方法

GradientLayer

创建一个GradientLayer然后添加到视图的

public extension UIView {

    func addGradientWithDirection(_ direction: GradientDirection, colors: [UIColor], cornerRadius: CGFloat = 0, locations: [Double]? = nil)

    func addGradient(_ gradientLayer: GradientLayer, cornerRadius: CGFloat = 0)
	
}

示例

let gradient = GradientLayer(direction: GradientDirection, colors: [UIColor])
view.addGradient(gradient)

UIGradient支持以下方向

public enum GradientDirection {
    case topToBottom // top to bottom
    case bottomToTop // bottom to top
    case leftToRight // left to right
    case rightToLeft // right to left
    case topLeftToBottomRight // top left to bottom right
    case topRightToBottomLeft // top right to bottom left
    case bottomLeftToTopRight // bottom left to top right
    case bottomRightToTopLeft // bottom right to top left
}

您也可以使用此函数创建 GradientLayer 的克隆实例

open class GradientLayer: CAGradientLayer {
    ...
    public final func clone() -> GradientLayer
    ...
}

渐变颜色

UIGradient 允许您创建渐变颜色

public extension UIColor {

    static func fromGradient(_ gradient: GradientLayer, frame: CGRect, cornerRadius: CGFloat = 0) -> UIColor?
	
    static func fromGradientWithDirection(_ direction: GradientDirection, frame: CGRect, colors: [UIColor], cornerRadius: CGFloat = 0, locations: [Double]? = nil) -> UIColor?
	
}

示例

let view1 = UIView(frame: frame)
view1.backgroundColor = UIColor.fromGradientWithDirection(.topToBottom, frame: frame, colors: [UIColor.blue, UIColor.green])
//view1.backgroundColor = UIColor.fromGradient(gradient, frame: frame)
let label = UILabel(frame: frame)
label.text = "some text"
label.textColor = UIColor.fromGradient(gradient, frame: frame)

渐变图像

UIGradient 还允许您创建渐变图像

public extension UIImage {

    static func fromGradient(_ gradient: GradientLayer, frame: CGRect, cornerRadius: CGFloat = 0) -> UIImage?
	
    static func fromGradientWithDirection(_ direction: GradientDirection, frame: CGRect, colors: [UIColor], cornerRadius: CGFloat = 0, locations: [Double]? = nil) -> UIImage?
	
}

示例

let imageView = UIImageView(frame: frame)
imageView.image = UIImage.fromGradientWithDirection(.bottomLeftToTopRight, frame: frame, colors: [UIColor.blue, UIColor.green])

渐变库

UIGradient 受益于 @lukedesigner 的启发。我根据他的 网站 创建了 24 种渐变,并将它们作为 GradientLayer 的静态变量,供大家在其项目中使用。

let oceanBlueGradient 		= GradientLayer.oceanBlue
let sanguineGradient  		= GradientLayer.sanguine
let lusciousLimeGradient 	= GradientLayer.lusciousLime
let purpleLakeGradient 		= GradientLayer.purpleLake
...

您可以在我的示例项目中探索它们。

作者

UIGradient 由 Đinh Quang Hiếu ([email protected]) 于 2017 年使用 Swift 创建。如果您发现任何问题,请创建问题或进行拉取请求以帮助我改进这个开源项目。谢谢!

许可

UIGradient 的发布和分发遵循 MIT 许可证条款和条件。