gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 01:03:04 27 4
gpt4 key购买 nike

我遇到了一个问题:在一个具有类似结构的更大项目中,我从 Instrument Tool 中得到内存泄漏。

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

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

我很感激任何答案。

在泄漏工具中有一个注释,负责的框架是:

[NSXPCConnection remoteObjectProxyWithErrorHandler:]

感谢阅读,示例代码如下:概览

动画计算

    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


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
}
}

内容 View


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")
})
}
}

用于复制粘贴

    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")
})
}
}

最佳答案

我可以使用以下代码重现相同的漏洞:

  import SwiftUI

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

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

在没有对此问题有更多见解的情况下,我认为您不应对泄漏负责,但 Apple 应该负责。

如果您仍然希望使用 SwiftUI,我认为您别无选择,只能暂时忽略泄漏。
顺便说一句,我在使用 SwiftUI 时遇到了更严重的问题,我暂时放弃了它,因为我认为它还没有准备好。

关于ios - SpriteKit 和 SwiftUI 动画(内存泄漏 - NSXPCConnection?),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67526778/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com