SwiftUI无法在实时Activity中显示SpriteView

lmyy7pcs  于 2023-05-05  发布在  Swift
关注(0)|答案(1)|浏览(122)

我正在尝试在应用程序中显示精灵视图。我已经有它显示为一个视图的主要应用程序和它的工作完全正常。当我尝试使用

var characterView: some View {
        SpriteView(scene: characterScene)
            .frame(width: 50, height: 50)
    }

展示这一景观。
下面是一些最小的代码:

// Character Animation
import Foundation
import SpriteKit

class CharacterScene: SKScene {
    let characterName: String
    init(characterName: String) {
        self.characterName = characterName
        super.init(size: CGSize(width: 300, height: 400))
    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    var textures: [SKTexture] = []
    override func didMove(to view: SKView) {
        let atlas = SKTextureAtlas(named: characterName)
        textures = atlas.textureNames.sorted().map { atlas.textureNamed($0) }
        
        let sprite = SKSpriteNode(texture: textures[0])
        let maxSize: CGFloat = 200
        let aspectRatio = sprite.size.height / sprite.size.width
        sprite.size = CGSize(width: min(sprite.size.width, maxSize), height: min(sprite.size.height, maxSize * aspectRatio))
        sprite.position = CGPoint(x: frame.midX, y: frame.midY)
        addChild(sprite)
        
        let animation = SKAction.animate(with: textures, timePerFrame: 0.5)
        sprite.run(SKAction.repeatForever(animation))
    }
}

//Widget

import WidgetKit
import SwiftUI
import ActivityKit
import SpriteKit

struct CharacterIslandWidget: Widget {
    var body: some WidgetConfiguration {
        ActivityConfiguration(
            for: CharacterIslandAttributes.self,
            content: { context in
                CharacterIslandWidgetView(context: context)
            },
            dynamicIsland: { context in
                DynamicIsland(
                    expanded: {
                        DynamicIslandExpandedRegion(.center) {
                            Text(context.state.CharacterName)
                        }
                        DynamicIslandExpandedRegion(.leading) {
                            SpriteView(scene: CharacterScene(characterName: context.state.CharacterImage))
                                .frame(width: 50, height: 50)
                        }
                    },
                    compactLeading: {
                        SpriteView(scene: CharacterScene(characterName: context.state.CharacterImage))
                            .frame(width: 50, height: 50)
                    },
                    compactTrailing: {
                    },
                    minimal: {
                        SpriteView(scene: CharacterScene(characterName: context.state.CharacterImage))
                            .frame(width: 50, height: 50)
                    })
            }
    )}
}
struct CharacterIslandWidgetView: View {
    let context: ActivityViewContext<CharacterIslandAttributes>
    var body: some View {
        SpriteView(scene: CharacterScene(characterName: context.state.CharacterImage))
            .frame(width: 50, height: 50)
        Text(context.state.CharacterImage)
    }
}

我没有在日志中得到任何可执行错误,所以我不知道确切的问题,但图像看起来像这样(我还没有为小部件设置预览)。

Map集名称在图像下方给出,并且是正确的,因为它在应用程序中运行得非常好。
一些额外的东西:

  • 资产文件夹在小部件和应用程序之间共享,它之所以能工作,是因为它会显示在应用程序上。
  • CharacterImage是包含所有精灵的Map集“文件夹”名称。

接下来我该怎么做?在Live Activities(Dynamic Island)中不允许使用精灵视图吗?
我尝试硬编码名称,并阅读错误日志,但没有显示。

n53p2ov0

n53p2ov01#

widget只支持SwiftUI视图的一个子集,而live activity是一种特殊的widget。苹果公司的文档SwiftUI views that can be used in widgets .
当尝试使用不受支持的SwiftUI视图(Why do some views appear as a red no entry sign in widgets?)时,似乎会显示红色禁止进入符号

相关问题