Skip to content

Is SwiftUI's UIHostingController causing me high energy impact? How to prevent it? #11

@Pranoy1c

Description

@Pranoy1c

I am using the this library for an animated gradient background in my app. It works fine.

However, my app is in Swift and so I am using UIHostingController to display the FluidGradient:

import SwiftUI
import FluidGradient

let childView = UIHostingController(rootView: GradientView2())
addChild(childView)
view.addSubview(childView.view)
childView.view.snp.makeConstraints { make in
    make.edges.equalToSuperview()
}
childView.didMove(toParent: self)


struct GradientView2: View {
    var body: some View {
        FluidGradient(blobs: [.red, .orange, .pink],
                      highlights: [.yellow, .orange, .purple],
                      speed: 0.2,
                      blur: 0.75)
          .background(.tertiary)
          .edgesIgnoringSafeArea(.all)
    }
}

The GitHub page of FluidGradient notes the following:

While it's easy to create the blob shapes in SwiftUI, animation in
SwiftUI is still performed on the CPU. This means that your app will
probably consume double-digit CPU percentages and be noted as a "high"
energy consumer in Activity Monitor.

We use CALayer instead because it offloads all the work to the
WindowServer, making your app have a zero performance impact despite
running the gradient animation at full screen refresh rate.

And they show that the FluidGradient doesn't cause ""high" energy consumer in Activity Monitor.".

However, in my app, Xcode is warning me of "HIGH" energy impact even though CPU usage is at 0%:

enter image description here

How can I prevent this? Does UIHostingController have high energy impact side effect?

Note that my app doesn't have anything else. Only this FluidGradient.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions