SwiftUI中的小部件UI预览

SwiftUI中的小部件UI预览,swiftui,widget,ios14,Swiftui,Widget,Ios14,如何将小部件预览插入应用程序 有人能给我一些建议吗?我用SwiftUI做了一个简单版本的小部件配置预览。我使用了一个带有小3D旋转的RoundedRectangle,并应用了阴影: 我将代码包含在名为WidgetView的自定义视图中: struct-WidgetView:View{ 让内容:内容 init(@ViewBuilder内容:()->content){ self.content=content() } var body:一些观点{ ZStack{ 圆角转角(圆角半径:24,样式:。

如何将小部件预览插入应用程序


有人能给我一些建议吗?

我用SwiftUI做了一个简单版本的小部件配置预览。我使用了一个带有小3D旋转的
RoundedRectangle
,并应用了阴影:

我将代码包含在名为
WidgetView
的自定义
视图中:

struct-WidgetView:View{
让内容:内容
init(@ViewBuilder内容:()->content){
self.content=content()
}
var body:一些观点{
ZStack{
圆角转角(圆角半径:24,样式:。连续)
.fill(颜色(.systemBackground))
内容
}
.框架(宽度:162,高度:162)
.旋转3D效果(.10度,轴:(x:1,y:1,z:0))
.阴影(颜色:颜色(UIColor.systemGray),半径:20,x:-5,y:10)
}
}
用法很简单。只需在小部件视图中添加所需的内容视图:

WidgetView{
HealthActiveEnergyView(条目:ActiveEnergyEntry.mock())
}
.preferredColorScheme(.light)
.预览布局(布局)
struct HealthActiveEnergyView:视图{
var条目:ActiveEnergyEntry
var body:一些观点{
VStack(对齐:。前导){
HStack{
文本(NSLocalizedString(“活动”,注释:))
.foregroundColor(entry.configuration.accentColor)
垫片()
图像(系统名称:“flame.fill”)
.font(.system(.title))
.foregroundColor(entry.configuration.accentColor)
}
文本(dateFormatter.string(from:entry.date))
.foregroundColor(颜色(.systemGray))
.font(.caption)
HStack(路线:.lastTextBaseline){
文本(\(entry.totalof千卡)”)
.font(.system(.largeTitle,设计:。四舍五入))
.bold()
.foregroundColor(颜色(.label))
文本(“千卡”)
.foregroundColor(颜色(.systemGray))
垫片()
}
垫片()
文本(NSLocalizedString(“平均7天”,注释:))
.foregroundColor(颜色(.systemGray))
.font(.caption)
HStack(路线:.lastTextBaseline){
文本(“\(entry.avgofkilocarries)”)
.font(.system(.callout,设计:。四舍五入))
.bold()
.foregroundColor(颜色(.label))
文本(“千卡”)
.foregroundColor(颜色(.systemGray))
.font(.caption)
垫片()
}
}
.padding()
}
}