Snippets Collections
https://medium.com/swlh/how-to-create-a-custom-gradient-in-swift-with-cagradientlayer-ios-swift-guide-190941cb3db2

lazy var gradient: CAGradientLayer = {
        let gradient = CAGradientLayer()
        gradient.type = .axial
        gradient.colors = [
            
            hexStringToUIColor(hex: "468d01"),
            hexStringToUIColor(hex: "acdc08")
        ]
        
        gradient.locations = [0,0.95]
        return gradient
    }()


func views() {
              gradient.frame = logInView.bounds
            logInView.layer.addSublayer(gradient)
            
            let x = logInView.frame.origin.x
            let y = logInView.frame.origin.y
            
            print("x = ", x)
            print("y = ", y)
            
            gradient.startPoint = CGPoint(x: 0, y: y)
            gradient.endPoint = CGPoint(x: 1, y: y)

}


    func hexStringToUIColor (hex:String) -> CGColor {
        var cString:String = hex.trimmingCharacters(in: .whitespacesAndNewlines).uppercased()

        if (cString.hasPrefix("#")) {
            cString.remove(at: cString.startIndex)
        }

        if ((cString.count) != 6) {
            return UIColor.gray.cgColor
        }

        var rgbValue:UInt64 = 0
        Scanner(string: cString).scanHexInt64(&rgbValue)

        return CGColor(
            red: CGFloat((rgbValue & 0xFF0000) >> 16) / 255.0,
            green: CGFloat((rgbValue & 0x00FF00) >> 8) / 255.0,
            blue: CGFloat(rgbValue & 0x0000FF) / 255.0,
            alpha: CGFloat(1.0)
        )
    }

star

Fri Sep 09 2022 07:26:02 GMT+0000 (UTC)

#ios #swift #gradient #gradent

Save snippets that work with our extensions

Available in the Chrome Web Store Get Firefox Add-on Get VS Code extension