SwiftUI Forever Animation:跟踪未检测到的内存泄漏

问题描述

以下.repeatForever动画正在循环播放时,内存使用量会增加,并且在动画停止时不会释放。除CGFontStrike分配堆积外,仪器看不到泄漏,也没有对堆栈进行排序的确切原因。

我尝试将动画放置在需要时有条件显示的单独视图结构中,但不会释放该内存。有关管理此视图的生命周期或跟踪我的错误的任何线索?

enter image description here

不确定是否重要:此视图的父视图托管在NSHostingView中,该NSHostingView是从macOS状态栏中显示的NSPopover的根。弹出/托管视图中的其他非重复SwiftUI动画不会导致相同的未释放内存增长。

enter image description here

enter image description here


import SwiftUI

struct QuitAppButton: View {
    @State var isHoveringOnQuit = false

    var body: some View {
        HStack(alignment: .center) {
            moon
                .overlay(sleepAnimation,alignment: .center)
                .overlay(labeloverlay.offset(x: 3,y: 21),alignment: .bottomTrailing)
        }
        .offset(x: 0,y: 2)
        .frame(minWidth: 15,alignment: .trailing)
        .padding(.vertical)
        .contentShape(Rectangle())
        .onHover { isHoveringOnQuit = $0 }
        .onTapGesture { NSApp.terminate(nil) }
        .focusable()
    }

    var labeloverlay: some View {
        Text("Quit")
            .font(.system(.footnote,design: .rounded))
            .foregroundColor(.buttonHover)
            .opacity(isHoveringOnQuit ? 1 : 0)
            .animation(.none)
            .frame(minWidth: 100,alignment: .trailing)
    }

    var moon: some View {
        Image(systemName: Symbols.moon.name)
            .imageScale(.large)
            .foregroundColor(isHoveringOnQuit
                                ? .buttonHover
                                : .buttonOff
            )
            .rotationEffect(Angle(degrees: isHoveringOnQuit ? -23 : 0),anchor: .center)
            .animation(.easeInOut(duration: 0.6),value: isHoveringOnQuit)
    }

    var zzz: some View {
        vstack(spacing: 0) {
            Text("Z")
                .offset(y: 3)
            Text("z")
                .offset(x: 8,y: -5)
            Text("z")
                .offset(x: 0,y: -13)
        }
        .offset(x: 1,y: -26)
        .font(.system(.caption,design: .rounded))
        .foregroundColor(.buttonOff)
    }

    var sleepAnimation: some View {
        zzz
            .scaleEffect(isHoveringOnQuit ? 0.73 : 0.45,anchor: .bottomLeading)
            .animation(isHoveringOnQuit
                        ? loopingScaling
                        : .default,value: isHoveringOnQuit)
            .opacity(isHoveringOnQuit ? 1 : 0)
            .animation(isHoveringOnQuit
                        ? loopingVisibility
                        : .default,value: isHoveringOnQuit)
            .opacity(isHoveringOnQuit ? 1 : 0)
            .animation(nonloopingShowAnimation,value: isHoveringOnQuit)
    }

    let loopingVisibility = Animation
        .linear(duration: 1.5)
        .repeatForever(autoreverses: true)

    let loopingScaling = Animation
        .linear(duration: 3)
        .repeatForever(autoreverses: false)

    let nonloopingShowAnimation = Animation.default
}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)