在我的類似 Tinder 的應用程式中,用戶會將影像上傳到他的個人資料中,以顯示給其他用戶。
最大照片為 3,一旦用戶上傳了一張照片,它將使用添加按鈕創建另一個“空白螢屏”。
代碼完美運行,但不會創建另一個影像上傳部分。
struct Person: Hashable, Identifiable {
var personImages: [UIImage?] = []
}
var person: Person
var body: some View {
HStack {
ForEach(0..<person.personImages.count) { imageIndex in
RoundedRectangle(cornerRadius: 20)
.frame(height: 4)
.foregroundColor(self.imageIndex == imageIndex ? Color.white : Color.gray.opacity(0.5))
}
}
}
這是什么imageIndex意思:
func updateImageIndex(addition: Bool) {
let newIndex: Int
if addition {
newIndex = imageIndex 1
} else {
newIndex = imageIndex - 1
}
imageIndex = min(max(0, newIndex), person.personImages.count - 1)
}
uj5u.com熱心網友回復:
我只能猜測......但我想你想要這樣的東西:
struct Person: Hashable {
var personImages: [UIImage?] = []
}
struct ContentView: View {
@State private var person = Person()
@State private var imageIndex: Int = 0
var body: some View {
VStack {
HStack {
ForEach(person.personImages, id:\.self) { image in
RoundedRectangle(cornerRadius: 20)
.frame(width: 60, height: 100)
.foregroundColor(.blue)
}
if person.personImages.count < 3 {
ZStack {
RoundedRectangle(cornerRadius: 20)
.frame(width: 60, height: 100)
.foregroundColor(.gray.opacity(0.5))
Button(" ") {
person.personImages.append(UIImage())
}
}
}
}
}
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/430371.html
