小编ear*_*org的帖子

iOS SwiftUI UIViewRepresentable updateUIView 找出哪些属性实际上发生了变化?

我想知道当调用 updateUIView 时如何测试 UIViewRepresentable 中的 @Binding 和 @StateObject 实际上发生了变化?

我正在实现 MKMapView,并且不想一直更新注释,仅当 StateObject 更改时才更新。

所以我想做这样的事情:

struct MapView: UIViewRepresentable
{
    @ObservedObject var annotations:MyAnnotations
    @ObservedObject var region:MKCoordinateRegion

    func updateUIView(_ view: MKMapView, context: Context)
    {
        if annotations.changed == true
        {
            // ... update annotations
        }
        
        if (region.changed == true
        {
            // ... update region
        }
    }
}
    
Run Code Online (Sandbox Code Playgroud)

一直更新注释会给渲染带来一些麻烦,我想避免这种情况。这可能是关于 UIViewRepresentables 的一个相当普遍的问题,旨在用于优化更新。

ios swift swiftui

6
推荐指数
1
解决办法
792
查看次数

标签 统计

ios ×1

swift ×1

swiftui ×1