मैंने निम्नलिखित उदाहरण का उपयोग करके स्विफ्टयूआई के साथ मैपबॉक्स को एकीकृत किया है:
https://github.com/mapbox/mapbox-maps-swiftui-demo
यह बढ़िया काम करता है। हालांकि व्यू स्टैक पर अन्य @State चर प्रदर्शित करने का प्रयास करते समय, यूआई रीफ्रेश प्रचार मैपबॉक्स कॉल updateUIView()
पर जाना बंद कर देता है
उदाहरण के लिए, आप उपरोक्त रिपॉजिटरी से ContentView.swift को निम्न कोड से बदलकर समस्या को दोहरा सकते हैं:
import SwiftUI
import Mapbox
struct ContentView: View {
@State var annotations: [MGLPointAnnotation] = [
MGLPointAnnotation(title: "Mapbox", coordinate: .init(latitude: 37.791434, longitude: -122.396267))
]
var body: some View {
ZStack {
VStack {
MapView(annotations: $annotations).centerCoordinate(.init(latitude: 37.791293, longitude: -122.396324)).zoomLevel(16)
Button(action: {
let rand = Float.random(in: 37.79...37.80)
self.annotations.append(MGLPointAnnotation(title: "Mapbox", coordinate: .init(latitude: CLLocationDegrees(rand), longitude: -122.396261)))
}) {
Text("Button")
Text("\(self.annotations.count)")
}
}
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
उपरोक्त कोड चलाने से संकेत मिलता है कि Text("\(self.annotations.count)")
UI अपडेट हो जाता है - हालांकि, एनोटेशन रीफ्रेश नहीं होते हैं (इसलिए updateUIView()
को कॉल नहीं किया जाता है)।
अगर मैं // Text("\(self.annotations.count)")
टिप्पणी करता हूं तो एनोटेशन रीफ्रेश हो जाते हैं (और updateUIView()
कहा जाता है)
क्या किसी के पास कोई विचार है कि समस्या क्या हो सकती है? या मुझसे यहां कुछ छूट रहा है?
धन्यवाद!
1 उत्तर
मेरे अपने प्रश्न का उत्तर यहाँ इस पोस्ट के लिए धन्यवाद
https://github.com/mapbox/mapbox-maps-swiftui-demo/issues/3#issuecomment-623905509
इसे काम करने के लिए मैपव्यू के अंदर प्रस्तुत किए जा रहे UIView को अपडेट करना आवश्यक है:
func updateUIView(_ uiView: MGLMapView, context: Context) {
updateAnnotations(uiView)
trackUser()
}
private func updateAnnotations(_ view: MGLMapView) {
if let currentAnnotations = view.annotations {
view.removeAnnotations(currentAnnotations)
}
view.addAnnotations(annotations)
}
संबंधित सवाल
नए सवाल
swiftui
SwiftUI ग्राफिकल यूजर इंटरफेस को परिभाषित करने के लिए एप्पल की घोषणात्मक स्विफ्ट एपीआई है। किसी भी प्लेटफ़ॉर्म पर SwiftUI के बारे में प्रश्नों के लिए इस टैग का उपयोग करें।