У меня есть Scrollview со списком элементов: < /p>
Код: Выделить всё
ScrollView {
VStack(spacing: 3) {
ForEach(testObjects) { obj in
ObjItem(obj: obj)
}
}
}
< /code>
objitem выглядит так: < /p>
struct ObjItem: View {
@State var pos: CGFloat = 0
@State var height: CGFloat = 100
var body: some View {
Image(uiImage: UIImage(named: obj.picture)!)
.resizable()
.aspectRatio(contentMode: .fill)
.frame(height: scrollPos: pos, fullHeight: width / 1280 * 800, alignment: .top)
.cornerRadius(5)
.onGeometryChange(for: CGRect.self) { proxy in
proxy.frame(in: .scrollView)
} action: {
pos = $0.minY
height = calcHeight(scrollPos: pos, fullHeight: $0.width / 1280 * 800)
}
}
}
< /code>
Я хочу динамически изменить высоту кадра элемента в зависимости от его положения в Scrollview: < /p>
func calcHeight(scrollPos: CGFloat, fullHeight: CGFloat) -> CGFloat {
if scrollPos < 0 {
return fullHeight
} else if scrollPos < fullHeight {
return 100 + (1 - scrollPos / fullHeight) * (fullHeight - 100)
} else {
return 100
}
}
.resizable()
.aspectRatio(contentMode: .fill)
.frame(height: calcHeight(pos: pos), alignment: .top)
.background {
GeometryReader { proxy in
Color.clear
(...)
}
}
< /code>
Это также работает, но заметно медленно на моем iPhone 14. Прокрутка заикает, но нет сбоев. Я совершенно ошибаюсь, используя .ongeometryChange или Geometry Reader? Есть более простое решение?
Подробнее здесь: https://stackoverflow.com/questions/796 ... f-elements
Мобильная версия