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

Day 94: max() and min() confusion

Forums > 100 Days of SwiftUI

Hi everyone i just ended the challenges for Layout and Geometry and achieved the desire end result but got confused because even though i used max() and min() like Paul suggested us to use, I think they're not doing anything in the code. Maybe I'm missing something or the way I did the challenges kind of makes max() and min() not needed?

Code below:

GeometryReader { fullView in
            ScrollView(.vertical) {
                ForEach(0..<50) { index in
                    GeometryReader { geo in
                        Text("Row #\(index)")
                            .frame(maxWidth: .infinity)
                            .background(Color(hue: min(geo.frame(in: .global).minY / fullView.frame(in: .global).maxY, 1.0), saturation: 1, brightness: 1))
                            .rotation3DEffect(.degrees(geo.frame(in: .global).minY - fullView.size.height / 2) / 5, axis: (x: 0, y: 1, z: 0))
                            .opacity(geo.frame(in: .global).minY / 200)
                            .scaleEffect(max(geo.frame(in: .global).minY / fullView.frame(in: .global).maxY + 0.5, 0.5))
                    .frame(height: 40)

For example, in the scaleEffect modifier, the way I calculate the number, it will go from 1.0 to 0 + 0.5, so it will never go below half the size of the Text view, but Paul asked us to use max() there to prevent exactly that...

The same thing for the Color hue, i used the same logic, so it will go from 1.0 to 0 but Paul asked to use min() to prevent the number from going beyond 1.0.

I still used both min and max but I'm afraid I'm missing something and don't want to get through this challenges thinking I did the correct thing but in reality did wrong.



what min and max did for me was to simplify my code. For example in challenge two Paul says not to go below 50%. I had this convoluted ternary code:

.scaleEffect((geo.frame(in: .global).origin.y / fullView.frame(in: .global).maxY * 2) < 0.5 ? 0.5 : geo.frame(in: .global).origin.y / fullView.frame(in: .global).maxY * 2)

and with max() I was able to simplfy to:

.scaleEffect(max(geo.frame(in: .global).origin.y / fullView.frame(in: .global).maxY * 2, 0.5))

and have the same effect. Does that help?


Hacking with Swift is sponsored by RevenueCat

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

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

Reply to this topic…

You need to create an account or log in to reply.

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.