WWDC23 SALE: Save 50% on all my Swift books and bundles! >>

Hacking with iOS SwiftUI - Project 18 Challenges - GeometryReader

Forums > Books

Has anybody had luck with the Project 18 challenges?

I've been trying several things, but I'll limit this post to one specific issue:

Challenge 2 starts with this code from Project 5, and challenges us to offset the words lower down in the list by using GeometryReader.

VStack {
     TextField("Enter your word", text: $newWord, onCommit: addNewWord)
             .textFieldStyle(RoundedBorderTextFieldStyle())
             .autocapitalization(.none)
             .padding()

     List(usedWords, id: \.self) {
             Image(systemName: "\($0.count).circle")
             Text($0)
     }
}

I've been able to offset the Text view based on its position in the List with the code below.

My issues are:

(1) how to access the value of each string from the usedWords array when creating the Text in the List? I can no longer simply use $0 to read the value once the Text is in a GeometryReader. Is there a way to enumerate the List so I can get the appropriate value for each word in the list?

(2) what's a good way to apply the offset only to words that are later in the list? If I have an enumerated list I could apply the offset only after a certain number of words. I've hacked it together here based on the word's position within the bounds of the entire screen.

VStack {
    TextField("Enter your word", text: $newWord, onCommit: addNewWord)
        .textFieldStyle(RoundedBorderTextFieldStyle())
        .autocapitalization(.none)
        .padding()

    GeometryReader { geo in
        List(usedWords, id: \.self) {
            Image(systemName: "\($0.count).circle")
            GeometryReader { textGeo in
                Text(usedWords[0])     //this is very wrong, we don't want to repeat just the first word for each item in the List
                    .offset(x: textGeo.frame(in: .global).midY > UIScreen.main.bounds.size.height * 0.7 ? (textGeo.frame(in: .global).midY/geo.size.height) * 20 : 0)
            }
        }
    }
}

Thanks in advance for any help!

1      

Save 50% in my WWDC23 sale.

SAVE 50% To celebrate WWDC23, all our books and bundles are half price, so you can take your Swift knowledge further without spending big! Get the Swift Power Pack to build your iOS career faster, get the Swift Platform Pack to builds apps for macOS, watchOS, and beyond, or get the Swift Plus Pack to learn advanced design patterns, testing skills, and more.

Save 50% on all our books and bundles!

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.