【发布时间】: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