Post

Replies

Boosts

Views

Activity

When using the animation, the CPU usage rises to around 20-25%.
I'm new to developing with SwiftUI and I created a Pomodoro app for macOS that runs in the menu bar. I added 4 animations and when the user selects the snow animation, it starts snowing on the screen. But the app uses 20%-30% of the CPU and has high energy consumption. I can't reduce it and I couldn't find a solution. // snow animation import SwiftUI struct SnowflakeView: View { @State private var flakeYPosition: CGFloat = -100 @State private var isAnimating = false private let flakeSize: CGFloat = CGFloat.random(in: 10...30) private let flakeColor: Color = Color( red: Double.random(in: 0.8...1), green: Double.random(in: 0.9...1), blue: Double.random(in: 1...1), opacity: Double.random(in: 0.6...0.8) ) private let animationDuration: Double = Double.random(in: 1...3) private let flakeXPosition: CGFloat = CGFloat.random(in: 0...310) var body: some View { Text("❄️") .font(.system(size: flakeSize)) .foregroundColor(flakeColor) .position(x: flakeXPosition, y: flakeYPosition) .onAppear { if !isAnimating { withAnimation(Animation.linear(duration: animationDuration).repeatForever(autoreverses: false)) { flakeYPosition = 280 + 50 } isAnimating = true } } } } I also have how I run the animation below. ZStack { ForEach(0..<10, id: \.self) { index in if selectedAnimal == "Snow" { SnowflakeView() } else if selectedAnimal == "Rain" { RainDropAnimation() }else if selectedAnimal == "Leaf"{ LeafFallAnimation() }else if selectedAnimal == "Confetti"{ ConfettiAnimation() } } }
0
0
273
Nov ’24