我想知道当调用 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 的一个相当普遍的问题,旨在用于优化更新。