SwiftUI ScrollView(的scrollIndicator)移动不需要的内容

e5nqia27  于 2024-01-05  发布在  Swift
关注(0)|答案(1)|浏览(119)
  1. import Foundation
  2. import SwiftUI
  3. struct TestChatUIView: View {
  4. @State var models: [TestChatModel2] = []
  5. init() {
  6. }
  7. var body: some View {
  8. ZStack(alignment: .top) {
  9. ScrollViewReader { scrollViewProxy in
  10. ScrollView {
  11. LazyVStack(spacing: 0) {
  12. ForEach(models.indices, id: \.self) { index in
  13. VStack {
  14. let message = models[index]
  15. Text("\(message.id)")
  16. .id(message.id)
  17. .flipped()
  18. .frame(minHeight: 40)
  19. .onAppear {
  20. if index < (3) {
  21. for i in 0...20 {
  22. let newModel = TestChatModel2(id: "id \(models.count + 1)", date: Date())
  23. models.insert(newModel, at: 0)
  24. }
  25. }
  26. }
  27. }
  28. }
  29. }
  30. }
  31. .flipped()
  32. }
  33. VStack {
  34. Spacer()
  35. Button {
  36. for int in 0...30 {
  37. models.insert(.init(id: "id \(models.count + 1)", date: Date()), at: 0)
  38. }
  39. } label: {
  40. Text("loadMore")
  41. .background(Color.red)
  42. }
  43. }
  44. }
  45. }
  46. }
  47. struct TestChatModel2: Identifiable {
  48. var id: String = UUID().uuidString
  49. var date: Date
  50. }
  51. extension View {
  52. func flipped() -> some View {
  53. self.rotationEffect(.radians(Double.pi))
  54. .scaleEffect(x: -1, y: 1, anchor: .center)
  55. }
  56. }

字符串
我正在制作聊天系统,这段代码是一个很好的例子。我想要的是当它接近底部时加载更多的消息。但问题是滚动焦点一直定位在底部,所以它导致递归加载更多功能。
如何修复?
我改变了.flipped()修改器的位置,就像在VStack上,ForEach,但它从来没有工作。

jjjwad0x

jjjwad0x1#

我调试了很多,只是发现这是不可行的。即使我使用UIKit(ScrollView+StackView和TableView,CollectionView)进行了相同的逻辑,所有相同的问题都发生了(当一些项目添加到顶部时,滚动移动到仍然顶部)
此外,一些聊天应用程序,如sendbird也无法解决此错误

  1. import Foundation
  2. import SwiftUI
  3. struct TestChatUIView: View {
  4. @State var models: [TestChatModel2] = []
  5. @State var modelId: String?
  6. var body: some View {
  7. ZStack(alignment: .top) {
  8. ScrollViewReader { scrollViewProxy in
  9. ScrollView {
  10. LazyVStack(spacing: 0) {
  11. ForEach(models, id: \.id) { message in
  12. Text("\(message.id)")
  13. .flipped()
  14. .frame(minHeight: 40)
  15. }
  16. }
  17. .scrollTargetLayout()
  18. }
  19. .scrollPosition(id: $modelId)
  20. .flipped()
  21. VStack {
  22. HStack {
  23. Button("Prepend") {
  24. loadPrev()
  25. }
  26. Button("Movee") {
  27. scrollViewProxy.scrollTo("id 31", anchor: .center)
  28. }
  29. Button("Append") {
  30. loadMore()
  31. }
  32. }
  33. }
  34. }
  35. .onAppear(perform: {
  36. for int in 0...30 {
  37. models.insert(.init(id: "id \(models.count + 1)", date: Date()), at: 0)
  38. }
  39. })
  40. }
  41. }
  42. func loadMore() {
  43. for i in 0...20 {
  44. let newModel = TestChatModel2(id: "id \(models.count + 1)", date: Date())
  45. models.insert(newModel, at: 0)
  46. }
  47. }
  48. func loadPrev() {
  49. for i in 0...20 {
  50. let newModel = TestChatModel2(id: "- id \(models.count + 1)", date: Date())
  51. models.append(newModel)
  52. }
  53. }
  54. }
  55. struct TestChatModel2: Identifiable, Equatable {
  56. var id: String = UUID().uuidString
  57. var date: Date
  58. }
  59. extension View {
  60. func flipped() -> some View {
  61. self.rotationEffect(.radians(Double.pi))
  62. .scaleEffect(x: -1, y: 1, anchor: .center)
  63. }
  64. }

字符串
但是在代码上面,我补充说。
.scrollTargetLayout().scrollPosition(id: $modelId)

**它为我工作(可悲的是,我的应用程序的目标是iOS 15)。

展开查看全部

相关问题