swiftui - 如何在 SwiftUI 中转换动画?
问题描述
在这里,我得到了 2 个形状,一个 Rectangle 和一个 Circle,通过 Button 的操作,其中只有一个对用户可见,我尝试使用 @Namespace 进行此转换,但没有成功!
我的目标:从一个 Shape 到另一个 Shape 的转换动画非常流畅。
struct ContentView: View {
@State var action: Bool = false
@Namespace var sameShape
var body: some View {
ZStack
{
Group
{
if action
{
Circle()
.fill(Color.blue).frame(width: 150, height: 150, alignment: .center)
.matchedGeometryEffect(id: "Dakota148Zero", in: sameShape)
}
else
{
Rectangle()
.fill(Color.red).frame(width: 150, height: 150, alignment: .center)
.matchedGeometryEffect(id: "Dakota148Zero", in: sameShape)
}
}
.animation(.easeInOut)
VStack
{
Spacer()
Button("transform") { action.toggle() }.font(Font.largeTitle).padding()
}
}
}
}
解决方案
Group
不是真正的容器,所以不要存储动画。Group
用一些堆栈替换,比如
VStack
{
if action
// ... other code no change
推荐阅读
- azure-active-directory - 在不存储客户端密码的情况下与 Azure AD OAuth 交互
- mysql - Mysql,如何组合连接查询的结果?
- javascript - 如何从外部 JSON 文件访问数据以在记事本中的 javascript 文件(没有 JQuery)中使用
- macos - 在 Python 中打开文件夹内的文件
- cross-domain - 当我使用 API 检索 GIF 时,跨源读取阻止正在阻止响应
- flutter - TabBar 和 TabView 没有 Scaffold 和固定的 Widget
- python - 如何使用 JSON 日期格式?
- android - Retrofit 无法在 Android 应用程序中下载二进制文件 - 最终结果是文件损坏,比预期大
- python - 循环图表仅选定的项目
- xpath - 使用 Scrapy 提取不带标签的文本序列