我有一段代码,可以通过手势放大和缩小一个有渐变的圆形。如果我把手指放在屏幕中央并进行缩放,这段代码能正常工作,但如果我把手指放在屏幕边缘并进行缩放,我希望它能够在我的手指之间的点上进行缩放。目前,它仍然以屏幕中心为放大中心来进行放大。
我该如何修改代码,使用户可以以恰好位于其手指位置之间的CGPoint为中心?
我该如何修改代码,使用户可以以恰好位于其手指位置之间的CGPoint为中心?
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)
}
}
}