I would like to execute a matched geometry effect. The initial view is clipped to either a rectangle or circle, and the overlay view is not clipped at all. How can I modify the matched geometry effect so that it smoothly transitions from a clipped view to no clip?
struct ContentView: View {
@State private var show = false
@Namespace var namespace
var body: some View {
VStack {
if !show {
KFImage(URL(string: "https://dfstudio-d420.kxcdn.com/wordpress/wp-content/uploads/2019/06/digital_camera_photo-1080x675.jpg"))
.resizable()
.aspectRatio(contentMode: .fill)
.matchedGeometryEffect(id: "squareAnim", in: namespace)
.frame(width: 37.5, height: 37.5)
.clipShape(Circle())
.onTapGesture {
withAnimation {
show.toggle()
}
}
}
}
.overlay {
if show {
KFImage(URL(string: "https://dfstudio-d420.kxcdn.com/wordpress/wp-content/uploads/2019/06/digital_camera_photo-1080x675.jpg"))
.resizable()
.aspectRatio(contentMode: .fill)
.matchedGeometryEffect(id: "squareAnim", in: namespace)
.frame(width: 150.0, height: 150.0)
.onTapGesture {
withAnimation {
show.toggle()
}
}
}
}
}
}
You could use the same techniques as shown in the answer to In Swift, how can I matchedGeometryEffect for a clipped() image without it glitching? (it was my answer).
ZStackto define the two positions.maxHeight(ormaxWidth) can be applied to theZStackto constrain the size of the large version.RoundedRectangle. In the small size, the corner radius should give a round shape. In the large size, a corner radius of 0 can be used..easeInOutis used, instead of the default.spring.