我遇到过UIViews的奇怪行为。我有一个空的'容器'UIView - WaveformPlot,预计将填充子子视图。
class WaveformPlot: UIView {
var normalColor: UIColor?
var progressColor: UIColor?
var waveforms: [UIView]?
override init(frame: CGRect) {
super.init(frame: frame)
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
waveforms = []
}
//MARK: Populate plot with data
func populateWithData(from dataSet: [Float]){
DispatchQueue.main.async {
var offset: CGFloat = 0
for index in 0..<dataSet.count {
let barView = UIView(frame: CGRect(x: offset,
y: self.frame.height / 2,
width: self.frame.width / CGFloat(dataSet.count),
height: -(CGFloat)((dataSet[index]))))
barView.backgroundColor = self.normalColor
barView.layer.borderColor = UIColor.black.cgColor
barView.layer.borderWidth = 0.25
//Append barView to parent 'container' view
self.addSubview(barView)
//Append barView to an array for future processing
self.waveforms?.append(barView)
offset += self.frame.width / CGFloat(dataSet.count)
}
}
}
func updateColorOfBars(with currentTime: Double, of totalDuration: Double){
let percentagePlayed = currentTime / totalDuration
print("Percerntage played - \((percentagePlayed * 100) / 100)")
let quantityOfBarsToBeUpdated = Double((self.waveforms?.count)!) * percentagePlayed
for item in self.waveforms!{
if (self.waveforms?.index(of: item)!)! <= Int(quantityOfBarsToBeUpdated * percentagePlayed) {
print("index processing - \(self.waveforms?.index(of: item))")
item.backgroundColor = self.progressColor
}
}
}
func clearPlot(){
for item in self.subviews{
item.removeFromSuperview()
}
}
}
然后我在ViewController中使用“data”[Float]数组中的64个值启动此WaveformPlot:
class ViewController: UIViewController {
var data: [Float] = [13, 37, 2, 3, 64, 62, 31, 50, 60, 17, 48, 56, 24, 25, 45, 8, 32, 1, 39, 26, 42, 10, 4, 27, 51, 40, 11, 12, 35, 41, 46, 21, 55, 36, 20, 14, 34, 47, 6, 9, 5, 59, 30, 54, 23, 49, 19, 0, 58, 61, 57, 28, 29, 15, 44, 16, 63, 18, 22, 43, 7, 33, 53, 38]
@IBOutlet weak var waveformView: WaveformPlot!
@IBOutlet weak var playButton: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
}
override func viewDidLayoutSubviews() {
waveformView.normalColor = UIColor.blue
waveformView.progressColor = UIColor.orange
waveformView.populateWithData(from: data)
}
}
这导致了这个 - &gt;
但在视觉调试期间,我发现有128个视图而不是64个视图。在每个barView下,还有另一个视图。 此屏幕截图的详细信息。
请帮我处理这个问题。为什么在原始栏中附加额外的栏?
答案 0 :(得分:1)
viewDidLayoutSubviews
将根据需要多次调用。
您每次调用时都会创建条形。
您应该迭代视图并在创建新视图之前将其删除:
for view in waveForms {
view.removeFromSuperview.
}
self.waveForms.removeAll()