感谢您花时间帮助他人:)
例如,我已经检查了所有解决方案这个帖子 https://stackoverflow.com/questions/61726424/swiftui-chat-app-the-woes-of-reversed-list-and-context-menu,但没有一个对我们有用。
也发表在苹果开发者 https://developer.apple.com/forums/thread/723770,我在这里发表也是希望好运。谢谢!
错误描述:
当我们构建聊天时,我们需要以与 List/ScrollView 相反的顺序显示消息。这就是为什么我们要把一切颠倒过来。
在 iOS 15 和 iOS 16(14 中没有)上,在反向 List/ScrollView 上显示 contextMenu 时会出现奇怪的行为(似乎是错误)。
根据您使用的方法,它甚至会消失。让我们深入了解一下。
一些注意事项:
反转用户界面的要点是像聊天列表一样,在底部显示最后一条消息,然后滚动到顶部,而不是正常(相反)的行为)。
- 我们确实尝试过反转数组,在出现时滚动到底部,但导致 iOS 14 上的scrollTo出现很多问题......性能不太好......
- 如果我们尝试反转 UI,是因为我们已经看到它非常高效,而且还因为没有额外的错误(显然,除了这篇文章中暴露的错误)
编写简单的演示代码来展示会发生什么。
import SwiftUI
struct ContentView: View {
var body: some View {
ScrollView { // It also happens using List (instead ScrollView + LazyVStack)
LazyVStack {
ForEach(1..<101, id: \.self) { msg in
messageView(msg)
.upsideDown() // Upside down each element, you can try .upsideDownBis()
}
}
}
.upsideDown() // Upside down entire ScrollView, you can try .upsideDownBis()
}
private func messageView(_ msg: Int) -> some View {
Text("Msg no. \(msg)")
.padding()
.background(Color.red.cornerRadius(8))
.contextMenu {
Button(action: {
print("A")
}) {
Text("Button A")
}
Button(action: {
print("B")
}) {
Text("Button B")
}
}
}
}
// Just for setting upside down list/ ScrollView
extension View {
func upsideDown() -> some View {
self
.rotationEffect(Angle(radians: .pi))
.scaleEffect(x: -1, y: 1, anchor: .center)
}
// This one just rotates, does not disappear.
func upsideDownBis() -> some View {
self
.rotationEffect(Angle(radians: .pi))
}
}
iOS 15 和 16 上此行为的 GIF 资源:
iOS 15/16 应用 upsideDown() 修饰符时 -> 错误:
iOS 15/16 when applying upsideDownBis() modifier -> Undesired animation:
我们检查了什么?
如前所述,每个解决方案发布于这个帖子 https://stackoverflow.com/questions/61726424/swiftui-chat-app-the-woes-of-reversed-list-and-context-menu和其他一些。除此之外的想法是:
- 由于scaleEffect确实导致了错误的最严重部分,因此在执行contextMenu手势时,我确实尝试将x:-1设置为x:1......但似乎比longPressGesture“识别器”更快。
- 与 1 类似,但在 upsideDownBis() 上尝试避免动画,将其设置为 nil,也不使修改器在这种情况下不应用...
因此,就目前情况而言,在 contextMenu 出现之前我无法识别 longPressGesture。
问题
- 有解决办法吗?不想用 upsideDownBis() 制作动画?
- 有解决办法吗?为了不让 upside Down() 出现这个错误?