GO FURTHER, FASTER: Try the Swift Career Accelerator today! >>

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)")
                            .font(.title)
                            .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.

Thanks.

3      

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?

4      

Hacking with Swift is sponsored by Essential Developer.

SPONSORED Transform your career with the iOS Lead Essentials. Unlock over 40 hours of expert training, mentorship, and community support to secure your place among the best devs. Click for early access to this limited offer and a FREE crash course.

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.