UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

Convert CALayerGradient to SwiftUI Gradient?

Forums > SwiftUI

Hi folks! I'm trying to recreate the Instagram app icon gradient in SwiftUI, and came across this code which replicated it perfectly:

var view = UILabel()
view.frame = CGRect(x: 0, y: 0, width: 1024, height: 1024)
view.backgroundColor = .white

let layer0 = CALayer()
layer0.backgroundColor = UIColor(red: 0.881, green: 0.106, blue: 0.496, alpha: 1).cgColor
layer0.bounds = view.bounds
layer0.position = view.center
view.layer.addSublayer(layer0)

let layer1 = CAGradientLayer()
layer1.colors = [
    UIColor(red: 0.259, green: 0.389, blue: 0.874, alpha: 1).cgColor,
    UIColor(red: 0.835, green: 0.208, blue: 0.522, alpha: 0).cgColor
]
layer1.locations = [0.04, 1]
layer1.startPoint = CGPoint(x: 0.25, y: 0.5)
layer1.endPoint = CGPoint(x: 0.75, y: 0.5)
layer1.transform = CATransform3DMakeAffineTransform(CGAffineTransform(a: 0.33, b: 0.9, c: -0.9, d: 0.33, tx: 0.56, ty: -0.16))
layer1.bounds = view.bounds.insetBy(dx: -0.5*view.bounds.size.width, dy: -0.5*view.bounds.size.height)
layer1.position = view.center
view.layer.addSublayer(layer1)

// + some more constraints stuff

However, as you see, this is using the Core Animation libarary, of which I have absolutely no knowledge, and couldn't possibly translate to SwiftUI Gradient code myself. Maybe I could use UIViewRepresentable? However, I want the resulting view / gradient to conform to ShapeStyle so that I can use it with .fill(_: ShapeStyle) on any Shape I want (I'm actually using a brilliant tool by Quassum Manus to convert SVG code to a SwiftUI Shape).

Many thanks!

2      

TAKE YOUR SKILLS TO THE NEXT LEVEL If you like Hacking with Swift, you'll love Hacking with Swift+ – it's my premium service where you can learn advanced Swift and SwiftUI, functional programming, algorithms, and more. Plus it comes with stacks of benefits, including monthly live streams, downloadable projects, a 20% discount on all books, and free gifts!

Find out more

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.