Although I know you'll again blame my naming conventions I decided to add a small example here: it does nothing except the peakmeter stuff and still causes a CPU load of 25% to 30% for 128 channels. This CPU load merely changes if you decrease the size of the window to only show two channels, while starting the example with only two channels (set s_NbPeakmeter = 2) shows a much lower cpu load. So invisible views also seem to cause CPU load?
import SwiftUI
class CPeakmeterManager: NSObject, ObservableObject
{
static public let s_NbPeakmeter: Int = 128
@Published var m_VecPeakmeterValues: [CGFloat] = []
var m_Timer: Timer? = nil
override init()
{
super.init()
m_VecPeakmeterValues = [CGFloat](repeating: 0.0, count: CPeakmeterManager.s_NbPeakmeter )
m_Timer = Timer.scheduledTimer(timeInterval: 0.05, target: self, selector: #selector( OnTimer ), userInfo: nil, repeats: true)
}
@objc func OnTimer()
{
for ChannelIndex in 0..<m_VecPeakmeterValues.count
{
m_VecPeakmeterValues[ ChannelIndex ] = CGFloat.random(in: 0...1)
}
}
}
struct PeakmeterView: View
{
@Binding var b_PeakmeterValue: CGFloat
var body: some View
{
GeometryReader { geometry in
ZStack(alignment: .trailing) {
HStack(spacing: 0) {
Rectangle()
.frame( width: 0.4 * geometry.size.width, height: 10 )
.foregroundColor(.green)
Rectangle()
.frame( width: 0.3 * geometry.size.width, height: 10 )
.foregroundColor(.yellow)
Rectangle()
.frame( width: 0.3 * geometry.size.width, height: 10 )
.foregroundColor(.red)
}
Rectangle()
.frame(width: min( ( 1.0 - b_PeakmeterValue ) * geometry.size.width, geometry.size.width), height: 10)
.opacity(0.9)
.foregroundColor(.gray)
}
}
}
}
@main
struct PeakmeterTestApp: App {
@StateObject var m_PeakmeterManager = CPeakmeterManager()
var body: some Scene {
WindowGroup {
ContentView().environmentObject(self.m_PeakmeterManager)
}
}
}
struct ContentView: View {
@EnvironmentObject var m_PeakmeterManager: CPeakmeterManager
var body: some View {
ScrollViewReader { proxy in
ScrollView {
ForEach(0 ..< CPeakmeterManager.s_NbPeakmeter, id: \.self) { ChannelIndex in
PeakmeterView( b_PeakmeterValue: $m_PeakmeterManager.m_VecPeakmeterValues[ ChannelIndex ])
.frame(width: 150)
}
}
}
.padding([.top,.bottom],12)
}
}