Inside a struct view in SwiftUI I have the following image element:
var body: some View {
GeometryReader { geometry in
VStack {
Image(imageName)
.resizable()
.aspectRatio(contentMode: .fit)
//.offset(zoomOffset)
// .scaleEffect(zoomScale, anchor: convertOffsetToUnitPoint(zoomOffset, in: geometry.size))
.animation(.easeInOut, value: currentSubRectIndex)
.contentShape(Rectangle())
.onTapGesture {
/*
Function call to offset and zoom goes here
*/
}
}
}
.edgesIgnoringSafeArea(.all)
}
I need a function accepts the top left corner and the bottom right corner of a new rectangle. The image should the move so that the center of the new rectangle is the center of the Image View. Then, the zoom in so that the new rectangle’s shorter dimension (width or height) fits the screen boundary. You can see where I’ve tried to change the offset
and scaleEffect
properties, but I’m not getting it right.
The user is not providing the rectangle, that is already stored. The user is only tapping the image to get the center/zoom effect to occur.
I have calculated the center, offset and zoom using simple math. I’ve been able to center the new rectangle, but I cannot get the zoom to work correctly. I think the zoom, is using the original center as the zoom reference, when I want it to zoom in on the center of the new rectangle (you can see my commented out efforts on the .scaleEffect
line.