SwiftUI:将 LazyVStack 或 LazyHStack 放入 ScrollView 会导致口吃(Apple 错误??) [英] SwiftUI: Putting a LazyVStack or LazyHStack in a ScrollView causes stuttering (Apple bug??)

查看:43
本文介绍了SwiftUI:将 LazyVStack 或 LazyHStack 放入 ScrollView 会导致口吃(Apple 错误??)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

XCode 版本 12.4 (12D4e)

XCode Version 12.4 (12D4e)

我每次在 ScrollView 中实现 Lazy 堆栈时都会遇到这种情况:

I have encountered this every time I have implemented a Lazy stack within a ScrollView:

  1. LazyHStack 添加到水平 ScrollView 或将 LazyVStack 添加到垂直 ScrollView
  2. 添加足够的内容,使滚动视图的内容大小超出其边界
  1. Add a LazyHStack to a horizontal ScrollView or a LazyVStack to a vertical ScrollView
  2. Add enough content such that the content size of the scroll view exceeds its bounds

场景 1 - 将滚动视图拉出边界(就像拉动以刷新)

预期行为:当滚动视图停留在您的手指之下时,它的行为与预期一致

Expected behavior: It behaves as expected where the scrollview stays under your finger

观察到的行为:口吃和跳跃

场景 2 - 快速滚动到边缘,使其必须反弹

预期行为:弹跳顺畅

观察到的行为:到达边缘时停止并抖动,但不反弹

Observed behavior: It stops and jitters when it reaches the edge, but doesn't bounce

我的理论我的理论是,由于使用了 Lazy 堆栈,当视图离开屏幕时,它会从视图层次结构中移除,从而造成卡顿.

My theory My theory is that due to using a Lazy stack, when a view goes off the screen it gets removed from the view hierarchy, creating a stutter.

我想知道有没有其他人遇到过这种情况?这是 SwiftUI 中的错误吗?几个月来,我已经在不同的项目中可靠地重现了这一点,但最终还是不使用我希望可以使用的 Lazy 堆栈.

I'm wondering if anyone else has encountered this? Is this a bug in SwiftUI? I've reliably reproduced this for months across different projects and end up resorting to not using Lazy stacks which I wish I could.

示例代码

        ScrollView {
          LazyVStack {
            ForEach(viewModel.items) { items in
              SomeView(viewModel: .init(context: viewModel.context, item: item))
            }
          }

注意:卡顿只发生在滚动视图的顶部

** 2021 年 7 月 10 日更新 **

这种情况在 iOS 15 版本 13.0 测试版 (13A5155e) 中仍然存在.

This is still happening in iOS 15, Version 13.0 beta (13A5155e).

在下面的视频中,注意滚动条的行为和到达底部时的卡顿:

In the video below, notice the behavior of the scrollbar and the stuttering when we get to the bottom:

https://youtu.be/z2pybl5yYqk

** 2021 年 7 月 19 日更新 **

我在我的视图中撕掉了所有东西,然后一个一个地重新构建它——只要我在一个简单的 Text<周围放置一个 VStack/HStack/ZStack,LazyVStack 就会开始卡顿/code> 元素.

I ripped everything out in my view and built it back up one by one — the LazyVStack begins to stutter as soon as I put a VStack/HStack/ZStack around a simple Text element.

如果我将 fixedSize(horizo​​ntal: false, vertical: true) 添加到 Text 元素,它似乎会停止口吃.一旦我添加了一个可变高度的 UIViewRepresentable,它就会再次开始卡顿.

If I add fixedSize(horizontal: false, vertical: true) to the Text element it seems to stop stuttering. As soon as I add a UIViewRepresentable of variable height, it starts to stutter again.

似乎在 LazyStack 中,每个孩子都需要某种固定大小或纯粹的 SwiftUI 视图才能工作.

It seems like in a LazyStack, every child needs to be some sort of fixed size or a purely SwiftUI view to work.

我会继续挖掘.必须……解决……

I'll keep digging in. Must... solve...

推荐答案

我收到了 DTS 的回复,他们确认这是一个错误,但没有解决方法.您可以参考我的反馈 ID 并提交反馈项目.我想他们会用新的 swift 版本解决这个问题,因为我认为这可能是一个遗留缺陷,可能会导致重大更改.换句话说,它与原生组件和导航栏有关,他们必须打破一些东西才能修复它.这意味着 iOS 14 中的 SwiftUI 应用程序可能永远不兼容.但我只是推测.如果我有任何消息,我会通知所有人.这真的是一个主要的障碍,完全破坏了 IMO 的用户体验.

I got a response from the DTS, and they confirmed it’s a bug, but no workaround. You can reference my feedback ID and file a feedback item. I imagine they’ll address it with the new swift version, because I think it’s probably a legacy defect, and might cause breaking changes. In other words, it has to do with the native components and navigation bar, and they have to break some things to fix it. This means SwiftUI apps in iOS 14 might not be compatible, ever. But I’m just speculating. I’ll let everyone know if I get any news. It’s really a major blocker, and completely ruins the user experience IMO.

这篇关于SwiftUI:将 LazyVStack 或 LazyHStack 放入 ScrollView 会导致口吃(Apple 错误??)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆