如何删除特定类型的所有子视图

3

我有一个疼痛地图,使用位置坐标来绘制疼痛部位。我可以在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)
        }
      }
    }

这个版本去掉了点,但只有一个点(self在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)
        }
      }
    }

如何在循环外添加多个点实例并将其移除?
1个回答

7

遍历地图的所有子视图,并删除所有的UIImageViews

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

如果您正在使用其他不想删除的UIImageView子视图,请创建子类UIImageViewclass MyDot:UIImage {...}):
for case let dot as MyDot in yourPainMapView.subViews

我今晚会尝试一下。当其他地方的按钮被按下时,能否调用点号为案例的点呢? - SashaZ
确保将“removeDots”作为操作添加到您的按钮。 - shallowThought

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接