I have been making an attempt to resolve the issue for a very long time, however I can not. I’ll actually admire your assist!
Proper now I am battling figuring out the coordinates (Offset) of the primary level of the a part of the picture I am seeing on the display.
For instance, this code prints the offset after the picture has been moved. And I do not perceive the right way to make it in order that when the picture is scaled, the offset adjustments accordingly. I imply that if I need to crop a photograph from the identical place, for instance on the left edge, however at totally different scales, I can not try this right here, as a result of the offset coordinates aren’t the identical.
struct ContentView: View {
@State non-public var imageScale: CGFloat = 1.0
@State non-public var previousScale: CGFloat = 0
@State non-public var imageOffset: CGSize = .zero
@State non-public var previousOffset: CGSize = .zero
var physique: some View {
let dragGesture = DragGesture()
.onChanged { worth in
self.imageOffset.width = worth.translation.width + self.previousOffset.width
self.imageOffset.top = worth.translation.top + self.previousOffset.top
}
.onEnded { worth in
self.previousOffset = self.imageOffset
print("Offset: (imageOffset)")
}
let pinchGesture = MagnificationGesture()
.onChanged { scale in
imageScale = scale + previousScale
}
.onEnded { _ in
previousScale = imageScale - 1
}
Picture(systemName: "particular person.fill")
.resizable()
.scaledToFill()
.scaleEffect(imageScale)
.offset(imageOffset)
.gesture(dragGesture)
.gesture(pinchGesture)
}
}