Ich habe eine Schmerzkarte, wo ich Standortkoordinaten verwende, um zu zeigen, wo Schmerzen aufgetreten sind. Ich kann die "Punkte" in einer For-In-Schleife hinzufügen und sie werden gut angezeigt. Ich kann sie jedoch nicht entfernen, bevor ich sie außerhalb der For-In-Schleife instanziiere. Jedes Mal, wenn ich die Ansicht aktualisiere, werden neue und nicht die alten angezeigt. Was kann ich tun?So entfernen Sie alle Unteransichten eines bestimmten Typs
Diese Version fügt die Punkte gut, aber ich kann sie nicht nach draußen entfernen, da ich nicht dot.removeFromSuperview nennen kann()
DispatchQueue.global(qos: .background).async {
if let locationNotNilX = self.painDiagramAnalysisModel.painLocationXFor(days: self.daysChosen){
x = locationNotNilX
count = locationNotNilX.count
}
if let locationNotNilY = self.painDiagramAnalysisModel.painLocationYFor(days: self.daysChosen){
y = locationNotNilY
}
let locationsArray = zip(x, y)
print("zipped array \(locationsArray)")
DispatchQueue.main.async {
let dot = UIImageView()
dot.removeFromSuperview()
dot.image = nil
for item in locationsArray {
self.locationPainY = (diagramHeight * CGFloat(item.1)) + originY
self.locationPainX = (diagramWidth * CGFloat(item.0)) + originX
print(" locationX \(self.locationPainX) locationY \(self.locationPainY)")
dot.image = UIImage(named: "dot")
dot.frame = CGRect(x: self.locationPainX - (dotDiameter/4), y: self.locationPainY - (dotDiameter/4), width: dotDiameter , height: dotDiameter)
if count > 2 {
dot.alpha = 0.6
} else {
dot.alpha = 1.0
}
dot.readingsPressedAnimation()
self.view.addSubview(dot)
}
}
}
Diese Version entfernt den Punkt, aber es gibt nur einen Punkt (selbst hängt an dem Punkt auf und instanziiert es nur einmal in der in Schleife.
let dot = UIImageView()
dot.removeFromSuperview()
dot.image = nil
DispatchQueue.global(qos: .background).async {
if let locationNotNilX = self.painDiagramAnalysisModel.painLocationXFor(days: self.daysChosen){
x = locationNotNilX
count = locationNotNilX.count
}
if let locationNotNilY = self.painDiagramAnalysisModel.painLocationYFor(days: self.daysChosen){
y = locationNotNilY
}
let locationsArray = zip(x, y)
print("zipped array \(locationsArray)")
DispatchQueue.main.async {
for item in locationsArray {
self.locationPainY = (diagramHeight * CGFloat(item.1)) + originY
self.locationPainX = (diagramWidth * CGFloat(item.0)) + originX
print(" locationX \(self.locationPainX) locationY \(self.locationPainY)")
dot.image = UIImage(named: "dot")
dot.frame = CGRect(x: self.locationPainX - (dotDiameter/4), y: self.locationPainY - (dotDiameter/4), width: dotDiameter , height: dotDiameter)
if count > 2 {
dot.alpha = 0.6
} else {
dot.alpha = 1.0
}
dot.readingsPressedAnimation()
self.view.addSubview(dot)
}
}
}
Wie kann ich viele Instanzen des Punktes und entfernen sie sie außerhalb der Schleife hinzufügen?
Ich werde versuchen. Kann das für den Fall Punkt genannt werden, wenn irgendwo anders eine Taste gedrückt wird? – SashaZ
sicher. Fügen Sie Ihrer Schaltfläche "removeDots" als Aktion hinzu. – shallowThought