gpt4 book ai didi

ios - 如何删除某种类型的所有 subview

转载 作者:搜寻专家 更新时间:2023-11-01 05:56:39 25 4
gpt4 key购买 nike

我有一张疼痛图,我在其中使用位置坐标来绘制疼痛部位。我可以在 for in 循环中添加“点”,它们显示得很好。但是,在 for in 循环之外实例化它们之前,我无法删除它们。因此,每次我更新 View 时,它都会绘制新 View ,而不是在旧 View 之上绘制新 View 。我能做什么?

这个版本很好地添加了点,但是我不能在外面删除它们,因为我不能调用 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 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)
}
}
}

如何在循环外添加点的多个实例并将它们移除?

最佳答案

遍历您的 map subview 并删除所有 UIImageViews:

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

如果您正在使用其他不想删除的 UIImageView subview ,子类 UIImageView (class MyDot:UIImage {...}):

for case let dot as MyDot in yourPainMapView.subViews

关于ios - 如何删除某种类型的所有 subview ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40466955/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com