【问题标题】:Performance when using a VStack inside a Scrollview (alternatives to List) in SwiftUI在 SwiftUI 中的 Scrollview(List 的替代品)中使用 VStack 时的性能
【发布时间】:2019-10-24 03:27:14
【问题描述】:

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

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

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

【问题讨论】:

  • SwiftUI 仍处于测试阶段,因此边缘仍然很粗糙。 SwiftUI 在其 DSL 中使用结构,但我很确定它的布局引擎仍然使用 UIViews 作为构建块。在 Apple 披露更多信息或开源(如果有的话)之前,我们不会知道更多信息。如果你关心性能,你可能想继续使用UIKit,也许你的视图控制器UIViewControllerRepresentable,所以你可以在SwiftUI中采用它。

标签: swift performance swiftui


【解决方案1】:

我偶然发现了一个类似的问题。发现使用 Metal 可能是保持性能的最佳解决方案。 .drawingGroup() 准确地说。请参阅here 了解更多说明。重要的是要记住,这会将视图转换为图像。所以像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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多