I have a piece of code that allows you to zoom in and out on a circle with a gradient using the magnification gesture. This works fine if I place my fingers in the middle of the screen and zoom, but if I place my fingers on the edge of the screen and do the magnification gesture, I want it to zoom in on the point in between my fingers. Right now, it still magnifies with the center of the screen as center for the magnification.
How can I modify my code to allow the users to center on the CGPoint right between the placement of their finger?
struct ContentView: View {
@GestureState var magnificationState = MagnificationState.inactive
@State var viewMagnificationState = CGFloat(1.0)
var magnificationScale: CGFloat {
return viewMagnificationState * magnificationState.scale
}
var body: some View {
let gradient = Gradient(colors: [.red, .yellow, .green, .blue, .purple, .red, .yellow, .green, .blue, .purple, .red, .yellow, .green, .blue, .purple])
let magnificationGesture = MagnificationGesture()
.updating($magnificationState) { value, state, transaction in
state = .zooming(scale: value)
}.onEnded { value in
self.viewMagnificationState *= value
}
Circle()
.fill(
RadialGradient(gradient: gradient, center: .center, startRadius: 50, endRadius: 2000)
)
.frame(width: 2000, height: 2000)
.scaleEffect(magnificationScale)
.gesture(magnificationGesture)
}
}
enum MagnificationState {
case inactive
case zooming(scale: CGFloat)
var scale: CGFloat {
switch self {
case .zooming(let scale):
return scale
default:
return CGFloat(1.0)
}
}
}