2016-11-07 2 views
1

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?

Antwort

6

Iterate über Ihre Karten Subviews und entfernen Sie alle UIImageViews:

func removeDots() { 
    for case let dot as UIImageView in yourPainMapView.subViews { 
     dot.removeFromSuperView() 
    } 
} 

Falls Sie verwenden andere UIImageView Subviews Sie wollen nicht entfernen, Unterklasse UIImageView (class MyDot:UIImage {...}): dass heute Abend

for case let dot as MyDot in yourPainMapView.subViews 
+0

Ich werde versuchen. Kann das für den Fall Punkt genannt werden, wenn irgendwo anders eine Taste gedrückt wird? – SashaZ

+0

sicher. Fügen Sie Ihrer Schaltfläche "removeDots" als Aktion hinzu. – shallowThought

Verwandte Themen