I am facing the same problem as below with SwiftUI.
Using PinchGesture; how can I zoom in to where a user's fingers actually "pinch"?
I think I can solve this problem by giving anchor
parameter of scaleEffect
the center coordinate of the two fingers.
But I don't know how to get the center.
My Code:
import SwiftUI
struct MagnificationGestureView: View {
@State var scale: CGFloat = 1.0
@State var lastScaleValue: CGFloat = 1.0
var body: some View {
Image(
systemName: "photo"
)
.resizable()
.frame(
width: 100,
height: 100
)
.scaleEffect(
self.scale
//, anchor: <---- give: the two fingers center position
)
.gesture(
magnification
)
}
var magnification: some Gesture {
// Can I get the center of the pinch?
MagnificationGesture().onChanged { val in
let delta = val / self.lastScaleValue
self.lastScaleValue = val
self.scale = self.scale * delta
}.onEnded { _ in
self.lastScaleValue = 1.0
}
}
}
Reference: https://stackoverflow.com/a/58468234/1979953