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

SOLVED: How to use a number to set UnitPoint for Color Gradient (not a Type property like ".leading")

Forums > SwiftUI

I'm trying to understand UnitPoint so I can set the startPoint and endPoint of a color linear Gradient with a number, like a CGFloat or a percent instead of the Property Type options I find in the documentation (.leading, .trailing, etc.). Note the code below builds for me:

 Rectangle()
                .fill(LinearGradient(gradient: Gradient(colors: [color1, color2]), startPoint: .top, endPoint: .bottom))

BUT I want to replace .top with an x, y coordinate, for example, like: startPoint: (x: 0,y: 0) I've tried percents, CGFloats, etc.

BTW, if anyone knows of a great tutorial or class to learn how to read the Documentation pages please share (gosh darn how I wish they always included sample code on these pages).

3      

UnitPoint(x: 0, y: 0) is .topLeading

UnitPoint(x: 1, y: 1) is .bottomTrailing

Use values between 0 and 1 to indicate points between those two corners.

5      

Hacking with Swift is sponsored by Essential Developer

SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until April 28th.

Click to save your free spot now

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.