在 SwiftUI 中的 Scrollview(列表的替代方案)中使用 VStack 时的性能

Sim*_*ckx 7 performance swift swiftui

使用内置列表视图,无法删除单元格之间的行分隔符。在我的设计中,我需要在每条线的右侧显示一个标签,有时甚至隐藏线。

目前唯一的解决方法是在 ScrollView 中使用 VStack。但是这样做时的性能如何?对于很长的 ScrollView 这样做是否安全?由于 SwiftUI 使用数据结构而不是 UIViews,我们真的需要重用行为吗?

在视频会议(https://developer.apple.com/videos/play/wwdc2019/237/)中,他们还解释了 SwiftUI 的布局行为,无法想象 ScrollView 中的一个非常长的 VStack 在需要计算每个单元格的高度......当然他们可以实现某种布局缓存。

Sim*_*mon 1

我偶然发现了类似的问题。发现使用 Metal 可能是保持性能的最佳解决方案。.drawingGroup()准确地说。请参阅此处以获取进一步说明。重要的是要记住,这会将视图转换为图像。因此像这样的主动控制TextField将不再起作用。可点击的区域仍然可以像动画一样工作。

/// Composites this view's contents into an offscreen image before final
/// display.
///
/// Views backed by native platform views don't render into the image.
/// Instead, they log a warning and display a placeholder image to highlight
/// the error.
///
/// - Parameters:
///   - opaque: A Boolean value that indicates whether the image is opaque.
///     If `true`, the alpha channel of the image must be one.
///   - colorMode: The working color space and storage format of the image.
/// - Returns: A view that composites this view's contents into an offscreen
///   image before display.
public func drawingGroup(opaque: Bool = false, colorMode: ColorRenderingMode = .nonLinear) -> some View
Run Code Online (Sandbox Code Playgroud)