如何删除某种类型的所有子视图

Sas*_*haZ 1 uiimageview ios swift swift3

我有一个疼痛贴图,我使用位置坐标来绘制疼痛的位置.我可以在for循环中添加"点",它们显示正常.但是,在我在for循环之外实例化它们之前,我无法删除它们.因此,每次我更新视图时,它都会绘制新视图而不是旧视图.我能做什么?

这个版本添加了很好的点,但我不能删除它们,因为我不能调用dot.removeFromSuperview()

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)
        }
      }
    }
Run Code Online (Sandbox Code Playgroud)

此版本删除了点,但只有一个点(自我挂在点上,只是在for循环中实例化它一次.

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)
        }
      }
    }
Run Code Online (Sandbox Code Playgroud)

如何添加点的多个实例并将其移除到循环外?

sha*_*ght 6

迭代您的地图子视图并删除所有UIImageViews:

func removeDots() {
    for case let dot as UIImageView in yourPainMapView.subViews {
        dot.removeFromSuperView()
    }
}
Run Code Online (Sandbox Code Playgroud)

如果您正在使用其他子UIImageView视图,则不希望删除子类UIImageView(class MyDot:UIImage {...}):

for case let dot as MyDot in yourPainMapView.subViews
Run Code Online (Sandbox Code Playgroud)