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!