【问题标题】:How to wrap a custom WKInterfaceImage for SwiftUI如何为 SwiftUI 包装自定义 WKInterfaceImage
【发布时间】:2020-07-25 11:11:45
【问题描述】:

SwiftUI 在提供WKInterfaceObjectRepresentable 方面做得很好但是,有没有办法为WKInterfaceImage 创建自定义视图?

这是我尝试过的,

import WatchKit
import SwiftUI
import SpriteKit

struct WatchImage: WKInterfaceObjectRepresentable {
    typealias WKInterfaceObjectType = WKInterfaceImage

    func makeWKInterfaceObject(context: WKInterfaceObjectRepresentableContext<WatchImage>) -> WKInterfaceImage {
        return WKInterfaceImage()
    }

    func updateWKInterfaceObject(_ wkInterfaceObject: WKInterfaceImage, context: Context) {
        wkInterfaceObject.setImage(UIImage(named: "start"))
   }
}

但代码未编译并在return WKInterfaceImage() 行出现错误并说init() 不可用

这可能是因为我们无法以编程方式创建WKInterfaceImage 的对象。我们需要将它与情节提要一起使用。

那么有什么解决方法吗?

【问题讨论】:

  • 你为什么不直接使用Image("start")WKInterfaceImage 这里的目的是什么?
  • 我想使用WKInterfaceImage的动画功能。我有许多图像,并使用它们来显示动画图像。 @Asperi

标签: ios swift swiftui watchkit


【解决方案1】:

苹果说,

不要自己继承或创建此类的实例。相反,在您的界面控制器类中定义插座并将它们连接到您的故事板文件中的相应对象。

https://developer.apple.com/documentation/watchkit/wkinterfaceimage

【讨论】:

  • 此选项在 SwiftUI 中不可用。 WKInterfaceImage 对象提供了比 SwiftUI Image 更多的用例(如动画)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-10-18
  • 2016-10-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多