SwiftUI ScrollView Прокрутка до относительной позиции
У меня большой горизонтальный вид, который не помещается на экране, поэтому я помещаю его в горизонтальный
ScrollView
. Если 0 будет представлять полностью прокрученный влево, а 1 - полностью вправо - как я могу, например, прокрутить до относительной позиции 0,8?
Поскольку я не могу прикрепить идентификатор к дочернему элементу
ScrollViewReader
не работает.
struct ContentView: View {
var body: some View {
ScrollView(.horizontal) {
Text("Example for a long view without individual elements that you could use id on. Example for a long view without individual elements that you could use id on. Example for a long view without individual elements that you could use id on. Example for a long view without individual elements that you could use id on. Example for a long view without individual elements that you could use id on. Example for a long view without individual elements that you could use id on. Example for a long view without individual elements that you could use id on. Example for a long view without individual elements that you could use id on. Example for a long view without individual elements that you could use id on. Example for a long view without individual elements that you could use id on.")
}
}
}
2 ответа
Решение
Вы можете решить эту проблему в чистом SwiftUI, используя
ZStack
с невидимым фоном:
struct ContentView: View {
var body: some View {
ScrollViewReader { reader in
ScrollView(.horizontal) {
ZStack {
HStack {
ForEach(0..<100) { i in
Spacer().id(i)
}
}
Text("Example for a long view without individual elements that you could use id on. Example for a long view without individual elements that you could use id on. Example for a long view without individual elements that you could use id on. Example for a long view without individual elements that you could use id on. Example for a long view without individual elements that you could use id on. Example for a long view without individual elements that you could use id on. Example for a long view without individual elements that you could use id on. Example for a long view without individual elements that you could use id on. Example for a long view without individual elements that you could use id on. Example for a long view without individual elements that you could use id on.")
}
}
Button("Go to 0.8") {
withAnimation {
reader.scrollTo(80)
}
}
}
}
}
С SwiftUI-Introspect, используемым для «изучения основных компонентов UIKit из SwiftUI», мы можем добиться этого.
Вот как это делается, в комплекте с
Slider
чтобы продемонстрировать его работу:
struct ContentView: View {
@State private var relativePosition: CGFloat = 0.5
var body: some View {
VStack {
ScrollView(.horizontal) {
Text("Example for a long view without individual elements that you could use id on. Example for a long view without individual elements that you could use id on. Example for a long view without individual elements that you could use id on. Example for a long view without individual elements that you could use id on. Example for a long view without individual elements that you could use id on. Example for a long view without individual elements that you could use id on. Example for a long view without individual elements that you could use id on. Example for a long view without individual elements that you could use id on. Example for a long view without individual elements that you could use id on. Example for a long view without individual elements that you could use id on.")
}
.introspectScrollView { scrollView in
let width = scrollView.contentSize.width - scrollView.frame.width
scrollView.contentOffset.x = relativePosition * width
}
let _ = relativePosition // Needed to update view
Slider(value: $relativePosition, in: 0 ... 1)
}
}
}