Использование ScrollView SwiftUI с вращениемIOS

Программируем под IOS
Anonymous
Использование ScrollView SwiftUI с вращением

Сообщение Anonymous »

Я разрабатываю приложение для чата.
Если я не поворачиваю ScrollView SwiftUI, возникает проблема, когда он прокручивается до 0-го индекса при загрузке истории чата. Итак, я повернул ScrollView и добавил разделитель, чтобы сообщения чата располагались сверху.
Однако есть проблема.
Когда добавляется новое сообщение чата, оно автоматически прокручивается вниз. Мне нужно, чтобы он не прокручивался автоматически, если пользователь читает сообщение посередине.
Кто-нибудь знает, как этого добиться? Вот мой код.
Знаете ли вы, как программно включить или отключить прокрутку при использовании ScrollView?
struct ChatView: View {
@State private var items: [Color] = [.red, .green, .blue, .yellow, .orange]
@State private var scrollViewHeight: CGFloat = 0
@State private var contentHeight: CGFloat = 0
@State private var isScrolledToBottom: Bool = true

var body: some View {
VStack {
ScrollViewReader { proxy in
ScrollView {

Spacer()
.frame(height: scrollViewHeight - contentHeight)

LazyVStack(spacing: 10) {

ForEach(items.indices, id: \.self) { index in
Rectangle()
.frame(height: 50)
.foregroundStyle(items[index])
.rotationEffect(.degrees(180))
.scaleEffect(x: -1, y: 1, anchor: .center)
}
.rotationEffect(.degrees(180))
.scaleEffect(x: -1, y: 1, anchor: .center)
}
.overlay(
GeometryReader { geometry in
Color.clear.preference(key: ContentHeightKey.self, value: geometry.size.height)
}
)
.onPreferenceChange(ContentHeightKey.self) { height in
print("ContentHeight: \(height)")
contentHeight = height
}

}
.onChange(of: items) { _ in

}
.overlay(
GeometryReader { geometry in
Color.clear
.preference(key: ViewHeightKey.self, value: geometry.size.height)
}
.onPreferenceChange(ViewHeightKey.self) { height in
print("scrollViewHeight: \(height)")
scrollViewHeight = height
}
)
.rotationEffect(.degrees(180))
.scaleEffect(x: -1, y: 1, anchor: .center)

.onAppear {
proxy.scrollTo(items.last, anchor: .bottom)
}
.onReceive(NotificationCenter.default.publisher(for: UIResponder.keyboardWillShowNotification)) { _ in
withAnimation {
proxy.scrollTo(items.last, anchor: .bottom)
}
}
}

HStack {
Button(action: {
items.insert(.black, at: 0)
}, label: {
Text("addItem")
})

Button(action: {
items.append(contentsOf: Array(repeating: .red, count: 5))
}, label: {
Text("LoadPreView")
})

}
}
}
}


Подробнее здесь: https://stackoverflow.com/questions/786 ... otation-in

Вернуться в «IOS»