Код: Выделить всё
struct ContentView: View {
@State var match = false
@Namespace var namespace
@State var snapshot:UIImage?
var body: some View {
ZStack {
if !match {
AView { img in
snapshot = img
withAnimation {
match.toggle()
}
}
.frame(width: 300, height: 300)
.matchedGeometryEffect(id: "1", in: namespace, properties: .frame)
}
if match && snapshot != nil {
Color.clear.ignoresSafeArea().overlay(
BView(image: snapshot!)
.matchedGeometryEffect(id: "1", in: namespace, properties: .frame)
.frame(width: 600, height: 600)
.transition(.scale)
.onTapGesture {
withAnimation {
match.toggle()
}
}
)
}
}
// .statusBar(hidden: true) // ()
var body: some View {
GeometryReader { geo in
ZStack(alignment: .center) {
Image("island")
.resizable()
.scaledToFill()
}
// .frame(width: geo.size.width, height: geo.size.height)
.border(.purple)
.onTapGesture {
didTap(snapshot(size: geo.size))
}
}
.border(.yellow)
}
}
struct BView: View {
@State var image:UIImage
var body: some View {
ZStack {
Color.red.ignoresSafeArea()
Image(uiImage: image)
.resizable()
.aspectRatio(contentMode: .fill)
}
}
}
extension View {
func snapshot(origin: CGPoint = .zero, size: CGSize = .zero) -> UIImage {
let controller = UIHostingController(rootView: self)
let view = controller.view
let targetSize = size == .zero ? controller.view.intrinsicContentSize : size
view?.backgroundColor = .clear
view?.bounds = CGRect(origin: origin, size: targetSize)
let renderer = UIGraphicsImageRenderer(size: targetSize)
return renderer.image { _ in
view?.drawHierarchy(in: controller.view.bounds, afterScreenUpdates: true)
}
}
}
Есть две вещи, почему они работают, как они делают:
В Bview (показан красным)? Я ожидал, что изображение заполнит bview и не имеет этого верхнего разрыва. Я попытался игнорировать область сохранения и т. Д., но без удачи.
Почему изображение в Aview не центрировано горизонтально? Центр.>
Подробнее здесь: https://stackoverflow.com/questions/729 ... gap-at-top
Мобильная версия