matchedGeometryEffect 无法正常工作 [英] matchedGeometryEffect doesn't work properly
本文介绍了matchedGeometryEffect 无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
使用此 Hacking with swift 文章,您会发现当 isZoomed
切换为 false 时,文本的动画效果不佳.我不明白为什么...
Using the last example from this Hacking with swift article you'll see that the text is not well animated when isZoomed
is toggled to false. I can't understand why...
struct ContentView: View {
@Namespace private var animation
@State private var isZoomed = false
var frame: CGFloat {
isZoomed ? 300 : 44
}
var body: some View {
VStack {
Spacer()
VStack {
HStack {
RoundedRectangle(cornerRadius: 10)
.fill(Color.blue)
.frame(width: frame, height: frame)
.padding(.top, isZoomed ? 20 : 0)
if isZoomed == false {
Text("Taylor Swift – 1989")
.matchedGeometryEffect(id: "AlbumTitle", in: animation)
.font(.headline)
Spacer()
}
}
if isZoomed == true {
Text("Taylor Swift – 1989")
.matchedGeometryEffect(id: "AlbumTitle", in: animation)
.font(.headline)
.padding(.bottom, 60)
Spacer()
}
}
.onTapGesture {
withAnimation(.spring()) {
self.isZoomed.toggle()
}
}
.padding()
.frame(maxWidth: .infinity)
.frame(height: isZoomed ? 400 : 60)
.background(Color(white: 0.9))
}
}
}
推荐答案
将每个可移除的部分包裹到自己的堆栈中,容器将动画正确移除视图.
Wrap every removable part into own stack, the container will animation removing views properly.
使用 Xcode 12.1/iOS 14.1 测试
Tested with Xcode 12.1 / iOS 14.1
VStack {
HStack {
RoundedRectangle(cornerRadius: 10)
.fill(Color.blue)
.frame(width: frame, height: frame)
.padding(.top, isZoomed ? 20 : 0)
HStack { // << here !!
if isZoomed == false {
Text("Taylor Swift – 1989")
.matchedGeometryEffect(id: "AlbumTitle", in: animation)
.font(.headline)
Spacer()
}}
}
VStack { // << here !!
if isZoomed == true {
Text("Taylor Swift – 1989")
.matchedGeometryEffect(id: "AlbumTitle", in: animation)
.font(.headline)
.padding(.bottom, 60)
Spacer()
}
}
}
这篇关于matchedGeometryEffect 无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文