简体   繁体   English

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

[英]How to remove all subviews of a certain type

I have a pain map where I use location coordinates to plot where there has been pain. 我有一个疼痛图,在其中我使用位置坐标来绘制出现疼痛的位置。 I can add the "dots" in a for in loop and they show up fine. 我可以在for in循环中添加“点”,它们显示得很好。 However I cannot remove them before I instantiate them outside the for in loop. 但是,我无法在for循环外实例化它们之前将其删除。 So every time I update the view it will plot new ones not ones on top of the old ones. 因此,每次更新视图时,它都会在旧视图之上绘制新视图而不是新视图。 What can I do? 我能做什么?

This version adds the dots well but I cannot remove them outside as I cannot call dot.removeFromSuperview() 这个版本很好地添加了点,但由于无法调用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)
        }
      }
    }

This version removes the dot but there is only one dot (self hangs on to the dot and just instantiates it once in the for in loop. 这个版本删除了点,但是只有一个点(自身挂在点上,仅在for in循环中实例化一次。

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)
        }
      }
    }

How can I add many instances of the dot and remove them outside the loop? 如何添加许多点实例并将其从循环中删除?

Iterate over your maps subviews and remove all UIImageViews : 遍历地图子视图并删除所有UIImageViews

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

In case you are using other UIImageView subViews you do not want to remove, subclass UIImageView ( class MyDot:UIImage {...} ): 如果你使用的是其他UIImageView子视图你不想删除,子类UIImageViewclass MyDot:UIImage {...}

for case let dot as MyDot in yourPainMapView.subViews

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM