加载View时,SwiftUI图像未更新其URL

我有一个UserLocation类的全局对象,该对象在AppDelegate.swift中初始化。我正在获取用户的当前位置,并根据用户的当前位置在SwiftUI视图中更改图像。 这是UserLocation类:

class UserLocation: ObservableObject {
      var userCurrentState: StateEnum = .None 
// StateEnum is an enum to store state areas like west,east and its default value is .None
}

在我的CoreLocation方法内部,经过一些计算,我设置了全局userCurrentState对象的userLocation

 func locationmanager(manager: CLLocationmanager,didUpdateLocations locations: [CLLocation])
    {

        * some calculations * 
        userLocation.userCurrentState = .West
    }

StateEnum的方法getStateImage返回一个String,它是应用程序xcassets中的图像名称,因此这是我在SwiftUI中使用它的方式查看:

import SwiftUI
import Combine

struct ContentView: View {

    @EnvironmentObject var locationOfUser: UserLocation 
    var body: some View {
        HStack {
            Image(locationOfUser.userCurrentState.getStateImage)
                    .resizable()
                    .frame(width: 30,height: 30,alignment: .leading)
        }
    }
    .
    .
    .
    .
    .
    struct ContentView_Previews: PreviewProvider {
           static var previews: some View {
                  ContentView().environmentObject(userLocation)
           }
    }

问题是,当我启动该应用程序时,显示为“无”状态的图像并且没有更改。我正在基于当前位置更改图像,一旦用户进入新区域并且用户状态被更改,图像就会更改,但我没有得到预期的行为。但是,如果在初始化视图之前间隔2毫秒,则图像会根据当前位置不断变化。当我启动应用程序并针对None枚举获取图像时,它只是没有变化。我观察到的是视图初始化和CoreLocation获得当前位置之间的微秒差异。该问题可以通过Timer解决,但我不想使用Timer或其他任何黑客手段。

nuaadoudou 回答:加载View时,SwiftUI图像未更新其URL

暂时没有好的解决方案,如果你有好的解决方案,请发邮件至:iooj@foxmail.com
本文链接:https://www.f2er.com/3038842.html

大家都在问