我正在制作使用匹配幾何效果的應用程式。它匹配嵌入在兩個不同視圖中的兩個矩形。它沒有按預期作業。請幫我。這是測驗視圖:
struct ContentViewTest: View {
@State var details = false
@Namespace var animation
var body: some View {
ZStack {
if !details {
TestView2(details: $details, anim: animation)
}
if details {
TestView1(details: $details, anim: animation)
}
}
}
}
這是TestView1:
struct TestView1: View {
@Binding var details: Bool
var anim: Namespace.ID
var body: some View {
ZStack{
Color.gray.ignoresSafeArea()
Color.red.frame(width: 300, height: 700)
.matchedGeometryEffect(id: "id1", in: anim)
.onTapGesture {
withAnimation {
details = false
}
}
}
}
}
這是TestView2:
struct TestView2: View {
@Binding var details: Bool
var anim: Namespace.ID
var body: some View {
ZStack{
Color.green.ignoresSafeArea()
Color.red.frame(width: 200, height: 200)
.matchedGeometryEffect(id: "id1", in: anim)
.onTapGesture {
withAnimation {
details = true
}
}
}
}
}
uj5u.com熱心網友回復:
只需添加properties位置(在兩個匹配的視圖中),如下所示
Color.red.frame(width: 300, height: 700)
.matchedGeometryEffect(id: "id1", in: anim, properties: .position) // << here !!
或(取決于您想要實作的效果)更改修飾符的順序(對于兩個視圖),例如
Color.red
.matchedGeometryEffect(id: "id1", in: anim) // << here !!
.frame(width: 300, height: 700)
使用 Xcode 13.2 / iOS 15.2 測驗
uj5u.com熱心網友回復:
這是針對您的問題的一種方法,在正確的位置使用帶有matchedGeometryEffect 的過渡:
struct ContentView: View {
@State var details = false
@Namespace var namespace
private let id: String = "myID"
var body: some View {
if (details) {
TestView1(details: $details, id: id, namespace: namespace)
}
else {
TestView2(details: $details, id: id, namespace: namespace)
}
}
}
struct TestView1: View {
@Binding var details: Bool
let id: String
var namespace: Namespace.ID
var body: some View {
ZStack {
Color.gray.ignoresSafeArea()
Color.red
.matchedGeometryEffect(id: id, in: namespace)
.transition(.scale(scale: 1.0))
.frame(width: 300, height: 700)
.onTapGesture {
withAnimation {
details = false
}
}
}
}
}
struct TestView2: View {
@Binding var details: Bool
let id: String
var namespace: Namespace.ID
var body: some View {
ZStack {
Color.green.ignoresSafeArea()
Color.red
.matchedGeometryEffect(id: id, in: namespace)
.transition(.scale(scale: 1.0))
.frame(width: 200, height: 200)
.onTapGesture {
withAnimation {
details = true
}
}
}
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/452277.html
上一篇:為CarPlay創建串列
