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()
}
}
}
Post
Replies
Boosts
Views
Activity
I am developing an application for macOS that runs exclusively in the menu bar, and in this application, I am using a ColorPicker to allow the user to choose a color. However, the ColorPicker is not working properly in the menu bar. What should I do to resolve this issue?