LAST CHANCE: Save 50% on all my Swift books and bundles! >>

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 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 July 28th.

Click to save your free spot now

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.