首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >SpriteKit和SwiftUI动画(内存泄漏- NSXPCConnection?)

SpriteKit和SwiftUI动画(内存泄漏- NSXPCConnection?)
EN

Stack Overflow用户
提问于 2021-05-13 22:05:34
回答 1查看 339关注 0票数 3

我有一个问题:在一个类似结构的大型项目中,我正在从仪器工具中获得内存泄漏。

如果您将其放入Xcode并运行,则应该会看到一行移动到右侧,然后向左移动。当按下按钮时,直线跳转到定义的位置。如果使用内存泄漏工具,则在单击Button后会出现错误。我也不知道原因。这是个虫子吗?代码中有基本错误吗?如何避免这种情况?

这是将动画计算与SwiftUI连接的正确方法吗?我将SKScene作为ObservableObject,并将动画标记为@已发布用于执行此动画吗?

我很感激你的回答。

在渗漏工具中有一个说明,即负责的框架是:

remoteObjectProxyWithErrorHandler: NSXPCConnection

谢谢您的阅读,下面是示例代码:概述

动画Calc

代码语言:javascript
复制
    import SwiftUI
    import SpriteKit

    struct MyAnimation{

        var lenght:CGFloat = 0  //Position of the line end
        var up: Bool = true  //if the line is moving to right or left

        mutating func change(){
        
            self.up ? (lenght += 1) : (lenght -= 1)
        
            if lenght > 100{
                up = false
            }else if lenght < 0{
                up = true
            }
        }
    }

用于更新SKScene的

代码语言:javascript
复制
    class GameScene: SKScene, ObservableObject{
    
        @Published var ani: MyAnimation  //handles the calculation
    
        override init(){
            ani = MyAnimation()
            super.init(size: CGSize(width: 200, height: 100))
        }
    
        required init?(coder aDecoder: NSCoder) {
            fatalError("init(coder:) has not been implemented")
        }
    
        override func update(_ currentTime: TimeInterval) {
            ani.change()  //new Position is calculated
        }
    }

内容视图

代码语言:javascript
复制
    struct ContentView: View {
    
        @StateObject var game = GameScene()
    
        var body: some View {
            VStack{
                ZStack{
                    SpriteView(scene: game).opacity(0)
                    MyPath().environmentObject(game)
                }
                Start().environmentObject(game)  
                //Button to let the line jump to the defined position
        
            }
        }
    }

路径以动画

代码语言:javascript
复制
    struct MyPath: View{
        
        @EnvironmentObject var game: GameScene
        
        var body: some View{
            Path{ path in
                path.move(to: CGPoint(x: 50, y: 50))
                path.addLine(to: CGPoint(x: 200 + game.ani.lenght, y: 220))  
                //here is the length property of the MyAnimation struct and should cause the redraw

                path.closeSubpath()
            }
            .stroke(Color.black, lineWidth: 4)
        }
    }

Button

代码语言:javascript
复制
    struct Start: View {    //Button
        
        @EnvironmentObject var game: GameScene
        
        var body: some View {
            Button(action: {
                game.isPaused = true
                game.ani.lenght = 30
                game.isPaused = false
            }, label: {
                Text("Start")
            })
        }
    }

用于复制粘贴

代码语言:javascript
复制
    import SwiftUI
    import SpriteKit

    struct MyAnimation{

        var lenght:CGFloat = 0  //Position of the line end
        var up: Bool = true  //if the line is moving to right or left

        mutating func change(){
        
            self.up ? (lenght += 1) : (lenght -= 1)
        
            if lenght > 100{
                up = false
            }else if lenght < 0{
                up = true
            }
        }
    }


    class GameScene: SKScene, ObservableObject{
    
        @Published var ani: MyAnimation  //handles the calculation
    
        override init(){
            ani = MyAnimation()
            super.init(size: CGSize(width: 200, height: 100))
        }
    
        required init?(coder aDecoder: NSCoder) {
            fatalError("init(coder:) has not been implemented")
        }
    
        override func update(_ currentTime: TimeInterval) {
            ani.change()  //new Position is calculated
        }
    }



    struct ContentView: View {
    
        @StateObject var game = GameScene()
    
        var body: some View {
            VStack{
                ZStack{
                    SpriteView(scene: game).opacity(0)
                    MyPath().environmentObject(game)
                }
                Start().environmentObject(game)  
                //Button to let the line jump to the defined position
        
            }
        }
    }


    struct MyPath: View{
        
        @EnvironmentObject var game: GameScene
        
        var body: some View{
            Path{ path in
                path.move(to: CGPoint(x: 50, y: 50))
                path.addLine(to: CGPoint(x: 200 + game.ani.lenght, y: 220))  
                //here is the length property of the MyAnimation struct and should cause the redraw

                path.closeSubpath()
            }
            .stroke(Color.black, lineWidth: 4)
        }
    }


    struct Start: View {    //Button
        
        @EnvironmentObject var game: GameScene
        
        var body: some View {
            Button(action: {
                game.isPaused = true
                game.ani.lenght = 30
                game.isPaused = false
            }, label: {
                Text("Start")
            })
        }
    }
EN

回答 1

Stack Overflow用户

发布于 2021-05-19 22:55:11

我可以用以下代码再现相同的泄漏:

代码语言:javascript
复制
  import SwiftUI

  struct ContentView: View {
    var body: some View {
        VStack{
            Start()
        }
    }
  }

  struct Start: View {
    var body: some View {
        Button(action: {
        }, label: {
            Text("Start")
        })
    }
  }

如果没有对这个问题有更多的见解,我会假设你不是泄漏的罪魁祸首,但苹果是。

如果您仍然希望使用SwiftUI,我认为您别无选择,只能暂时忽略漏洞。

顺便说一句,我在SwiftUI上遇到了更糟的问题,现在我已经放弃了它,因为我相信它还没有准备好。

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67526778

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档