我只是嘗試使用matchedGeometryEffect 制作一些影片。
但是,存在一個錯誤,即物件是基于右下角而不是中心進行影片處理的。
我使用的代碼在這里。:
import SwiftUI
struct Test: View {
@Namespace private var animation
@State var show = false
var body: some View {
HStack {
if show != true {
Rectangle()
.frame(width: 50, height: 50)
.matchedGeometryEffect(id: "animation", in: animation)
}
Spacer()
Button(action: {
withAnimation {
show.toggle()
}
}) {
Text("Switch")
}
Spacer()
if show {
Rectangle()
.frame(width: 200, height: 200)
.matchedGeometryEffect(id: "animation", in: animation)
}
}
}
}
struct Test_Previews: PreviewProvider {
static var previews: some View {
Test()
}
}
使用這些代碼,我得到了這個結果。:

本來,我是想讓它們像這樣動起來。(從
如您所見,與我的不同,它們基于中心點移動。
我無法弄清楚是什么問題。
我使用 Image、Text 事物在案例中對其進行了測驗,但它的行為方式相同。
這只發生在我的筆記本電腦上嗎?
如果您能告訴我這里有什么問題,我將不勝感激。
uj5u.com熱心網友回復:
在固定視圖下方找到 - 修飾符的順序很重要。使用 Xcode 13 / iOS 14 測驗
注意:激活慢速影片以獲得更好的可見性

struct Test: View {
@Namespace private var ns
@State var show = false
var body: some View {
HStack {
if show != true {
Rectangle()
.matchedGeometryEffect(id: "animation", in: ns) // << here !!
.frame(width: 50, height: 50)
}
Spacer()
Button(action: {
withAnimation {
show.toggle()
}
}) {
Text("Switch")
}
Spacer()
if show {
Rectangle()
.matchedGeometryEffect(id: "animation", in: ns) // << here !!
.frame(width: 200, height: 200)
}
}
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/338358.html
