matchedGeometryEffect iOS14的新功能 改变视图大小和位置
struct MatchedGeometryEffectDemo: View {
@State private var isExpanded = false
@Namespace private var namespace // 命名空间
var body: some View {
ZStack {
if isExpanded {
VStack {
RoundedRectangle(cornerRadius: 10)
.foregroundColor(Color.pink)
.frame(width: 60, height: 60)
.matchedGeometryEffect(id: "rect", in: namespace)
Text("Hello SwiftUI!").fontWeight(.semibold)
.matchedGeometryEffect(id: "text", in: namespace)
}
} else {
HStack {
Text("Hello SwiftUI!").fontWeight(.semibold)
.matchedGeometryEffect(id: "text", in: namespace)
RoundedRectangle(cornerRadius: 10)
.foregroundColor(Color.pink)
.frame(width: 60, height: 60)
.matchedGeometryEffect(id: "rect", in: namespace)
}
}
}
.onTapGesture {
withAnimation {
isExpanded.toggle()
}
}
}
}
网友评论